Skip to content

Commit 705a33f

Browse files
palindrome linked list
1 parent a19da13 commit 705a33f

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package easy;
2+
3+
import java.util.Stack;
4+
5+
import utils.CommonUtils;
6+
import classes.ListNode;
7+
8+
public class PalindromeLinkedList {
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+
public boolean isPalindrome_O1_space(ListNode head) {
13+
if(head == null) return true;
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+
ListNode slow = 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+
ListNode reversedHead = reverse(slow.next);
24+
CommonUtils.printList(reversedHead);
25+
CommonUtils.printList(head);
26+
ListNode firstHalfHead = head;
27+
while(firstHalfHead != null && reversedHead != null){
28+
if(firstHalfHead.val != reversedHead.val) return false;
29+
firstHalfHead = firstHalfHead.next;
30+
reversedHead = reversedHead.next;
31+
}
32+
return true;
33+
}
34+
35+
private ListNode reverse(ListNode head) {
36+
ListNode pre = null;
37+
while(head != null){
38+
ListNode next = head.next;
39+
head.next = pre;
40+
pre = head;
41+
head = next;
42+
}
43+
return pre;
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+
public boolean isPalindrome(ListNode head) {
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+
ListNode temp = head;
54+
int count = 0;
55+
while(temp != null){
56+
count++;
57+
temp = temp.next;
58+
}
59+
boolean lengthIsEven = (count%2 == 0);
60+
Stack<Integer> stack = new Stack();
61+
temp = head;
62+
for(int i = 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) return false;
70+
temp = temp.next;
71+
}
72+
return true;
73+
}
74+
75+
public static void main(String...strings){
76+
PalindromeLinkedList test = new PalindromeLinkedList();
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+
ListNode head = new ListNode(1);
84+
CommonUtils.printList(head);
85+
// ListNode result = test.reverseList_iterative(head);
86+
// Boolean result = test.isPalindrome(head);
87+
Boolean result = test.isPalindrome_O1_space(head);
88+
System.out.println(result);
89+
}
90+
91+
}

0 commit comments

Comments
 (0)