|
| 1 | +packageeasy; |
| 2 | + |
| 3 | +importjava.util.Stack; |
| 4 | + |
| 5 | +importutils.CommonUtils; |
| 6 | +importclasses.ListNode; |
| 7 | + |
| 8 | +publicclassPalindromeLinkedList { |
| 9 | +//then I turned to Discuss, and found that they actually reverse the half and then do the comparison, e.g. https://discuss.leetcode.com/topic/33376/java-easy-to-understand |
| 10 | +//a strong candidate would try to restore the reversed half before return to keep the input intact |
| 11 | +//practice does make perfect! Cheers! I implemented this code in 20 mins this time! Cheers! |
| 12 | +publicbooleanisPalindrome_O1_space(ListNodehead) { |
| 13 | +if(head ==null)returntrue; |
| 14 | +//how to get to middle node in a list? a typical trick is to use slow/fast pointers, when fast reaches the end, slow arrives at the middle |
| 15 | +ListNodeslow =head,fast =head; |
| 16 | +while(fast.next !=null &&fast.next.next !=null){ |
| 17 | +fast =fast.next.next; |
| 18 | +slow =slow.next; |
| 19 | + } |
| 20 | +//if we exit due to fast.next == null, that means the length of this list is odd, |
| 21 | +//if it's due to fast.next.next == null, then it's even |
| 22 | +//actually it doesn't matter whether the length if odd or even, we'll always use slow as the newHead to reverse the second half |
| 23 | +ListNodereversedHead =reverse(slow.next); |
| 24 | +CommonUtils.printList(reversedHead); |
| 25 | +CommonUtils.printList(head); |
| 26 | +ListNodefirstHalfHead =head; |
| 27 | +while(firstHalfHead !=null &&reversedHead !=null){ |
| 28 | +if(firstHalfHead.val !=reversedHead.val)returnfalse; |
| 29 | +firstHalfHead =firstHalfHead.next; |
| 30 | +reversedHead =reversedHead.next; |
| 31 | + } |
| 32 | +returntrue; |
| 33 | + } |
| 34 | + |
| 35 | +privateListNodereverse(ListNodehead) { |
| 36 | +ListNodepre =null; |
| 37 | +while(head !=null){ |
| 38 | +ListNodenext =head.next; |
| 39 | +head.next =pre; |
| 40 | +pre =head; |
| 41 | +head =next; |
| 42 | + } |
| 43 | +returnpre; |
| 44 | + } |
| 45 | + |
| 46 | +//I could only think of solutions that use O(n) space: store half of the nodes values |
| 47 | +//I don't know how Two Pointers technique could achieve O(1) effect |
| 48 | +publicbooleanisPalindrome(ListNodehead) { |
| 49 | +//let's get it AC'ed first |
| 50 | +//truely, I got this one AC'ed the first time I submitted it, cheers! |
| 51 | + |
| 52 | +//get the length of the list first |
| 53 | +ListNodetemp =head; |
| 54 | +intcount =0; |
| 55 | +while(temp !=null){ |
| 56 | +count++; |
| 57 | +temp =temp.next; |
| 58 | + } |
| 59 | +booleanlengthIsEven = (count%2 ==0); |
| 60 | +Stack<Integer>stack =newStack(); |
| 61 | +temp =head; |
| 62 | +for(inti =0;i <count/2;i++){ |
| 63 | +stack.push(temp.val); |
| 64 | +temp =temp.next; |
| 65 | + } |
| 66 | + |
| 67 | +if(!lengthIsEven)temp =temp.next; |
| 68 | +while(!stack.isEmpty()){ |
| 69 | +if(stack.pop() !=temp.val)returnfalse; |
| 70 | +temp =temp.next; |
| 71 | + } |
| 72 | +returntrue; |
| 73 | + } |
| 74 | + |
| 75 | +publicstaticvoidmain(String...strings){ |
| 76 | +PalindromeLinkedListtest =newPalindromeLinkedList(); |
| 77 | +// ListNode head = new ListNode(1); |
| 78 | +// head.next = new ListNode(2); |
| 79 | +// head.next.next = new ListNode(3); |
| 80 | +// head.next.next.next = new ListNode(4); |
| 81 | +// head.next.next.next.next = new ListNode(5); |
| 82 | + |
| 83 | +ListNodehead =newListNode(1); |
| 84 | +CommonUtils.printList(head); |
| 85 | +// ListNode result = test.reverseList_iterative(head); |
| 86 | +// Boolean result = test.isPalindrome(head); |
| 87 | +Booleanresult =test.isPalindrome_O1_space(head); |
| 88 | +System.out.println(result); |
| 89 | + } |
| 90 | + |
| 91 | +} |