/
githubmirror
/
interviews
Обзор
Документация
Войти
/
githubmirror
/
interviews
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
leetcode/linked-list/PalindromeLinkedList.java
40 строк
892 B
Kevin Naughton Jr
finish renaming files and directories
27 мар 2018, 19:52
27 мар 2018, 19:52
ec6dfb5
Код
Авторство
О чём код?
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class PalindromeLinkedList { public boolean isPalindrome(ListNode head) { if(head == null || head.next == null) { return true; } Stack<Integer> stack = new Stack<Integer>(); ListNode fast = head; ListNode slow = head; while(fast != null && fast.next != null) { stack.push(slow.val); fast = fast.next.next; slow = slow.next; } if(fast != null) { slow = slow.next; } while(slow != null) { if(stack.pop() != slow.val) { return false; } slow = slow.next; } return true; } }