Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static boolean isPalindrome(LinkedList list){
- LinkedListNode slow = list.head;
- LinkedListNode fast = list.head;
- Stack<Integer> stack = new Stack<Integer>();
- while(fast!=null&&fast.next!=null){
- stack.push(slow.value);
- slow = slow.next;
- fast = fast.next.next;
- }
- /*if the length of list is odd*/
- if(fast!=null){
- slow = slow.next;
- }
- /*compare elements all the way to the end*/
- while(slow!=null){
- if(slow.value!=stack.pop().intValue()){
- return false;
- }
- slow = slow.next;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement