Palindrome Linked List – Leetcode Java | Welkin Lan
Given a singly linked list, determine if it is a palindrome.
Follow up:
Could you do it in O(n) time and O(1) space?
1. reverse the second half
2. compare 1 with the first half
https://leetcode.com/discuss/78152/java-easy-to-understand
def isPalindrome(self, head): rev = None fast = head while fast and fast.next: fast = fast.next.next rev, rev.next, head = head, rev, head.next tail = head.next if fast else head isPali = True while rev: isPali = isPali and rev.val == tail.val head, head.next, rev = rev, head, rev.next tail = tail.next return isPali
https://github.com/shawnfan/LintCode/blob/master/Java/Palindrome%20Linked%20List.java
http://www.jiuzhang.com/solutions/palindrome-linked-list/
public boolean isPalindrome(ListNode head) { if(head==null) return true; ListNode fast = head; ListNode slow = head; ListNode pre=null; ListNode next = null; while(fast!=null&&fast.next!=null){ fast = fast.next.next;// 此步一定要在链表逆转操作之前执行 next = slow.next; slow.next = pre; pre = slow; slow = next; } if(fast!=null&&fast.next==null){ // 表示链表长度为奇数,则从中间节点的下一节点开始比较 slow = slow.next; } while(pre!=null&&slow!=null){ if(pre.val!=slow.val) return false; pre = pre.next; slow = slow.next; } return true; }
http://www.cnblogs.com/theskulls/p/4940695.html
https://leetcode.com/discuss/49428/concise-o-n-o-n-java-solution-without-reversing-the-list
https://leetcode.com/discuss/52685/share-my-java-answer
Since it's a recursive algorithm, it uses O(N) space.
https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2002.%20Linked%20Lists/Q2_06_Palindrome
https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2002.%20Linked%20Lists/Q2_06_Palindrome/QuestionC.java
public LinkedListNode node;
public boolean result;
public Result(LinkedListNode n, boolean res) {
node = n;
result = res;
}
}
public static Result isPalindromeRecurse(LinkedListNode head, int length) {
if (head == null || length <= 0) { // Even number of nodes
return new Result(head, true);
} else if (length == 1) { // Odd number of nodes
return new Result(head.next, true);
}
/* Recurse on sublist. */
Result res = isPalindromeRecurse(head.next, length - 2);
/* If child calls are not a palindrome, pass back up
* a failure. */
if (!res.result || res.node == null) {
return res;
}
/* Check if matches corresponding node on other side. */
res.result = (head.data == res.node.data);
/* Return corresponding node. */
res.node = res.node.next;
return res;
}
public static int lengthOfList(LinkedListNode n) {
int size = 0;
while (n != null) {
size++;
n = n.next;
}
return size;
}
public static boolean isPalindrome(LinkedListNode head) {
int length = lengthOfList(head);
Result p = isPalindromeRecurse(head, length);
return p.result;
}
https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2002.%20Linked%20Lists/Q2_06_Palindrome/QuestionB.java
public static boolean isPalindrome(LinkedListNode head) {
LinkedListNode fast = head;
LinkedListNode slow = head;
Stack<Integer> stack = new Stack<Integer>();
while (fast != null && fast.next != null) {
stack.push(slow.data);
slow = slow.next;
fast = fast.next.next;
}
/* Has odd number of elements, so skip the middle */
if (fast != null) {
slow = slow.next;
}
while (slow != null) {
int top = stack.pop().intValue();
if (top != slow.data) {
return false;
}
slow = slow.next;
}
return true;
}
https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2002.%20Linked%20Lists/Q2_06_Palindrome/QuestionA.java
public static boolean isPalindrome(LinkedListNode head) {
LinkedListNode reversed = reverseAndClone(head);
return isEqual(head, reversed);
}
public static LinkedListNode reverseAndClone(LinkedListNode node) {
LinkedListNode head = null;
while (node != null) {
LinkedListNode n = new LinkedListNode(node.data); // Clone
n.next = head;
head = n;
node = node.next;
}
return head;
}
public static boolean isEqual(LinkedListNode one, LinkedListNode two) {
while (one != null && two != null) {
if (one.data != two.data) {
return false;
}
one = one.next;
two = two.next;
}
return one == null && two == null;
}
https://leetcode.com/discuss/45309/reversing-a-list-is-not-considered-o-1-space
Read full article from Palindrome Linked List – Leetcode Java | Welkin Lan
Given a singly linked list, determine if it is a palindrome.
Follow up:
Could you do it in O(n) time and O(1) space?
1. reverse the second half
2. compare 1 with the first half
https://leetcode.com/discuss/78152/java-easy-to-understand
public boolean isPalindrome(ListNode head) {
if (head == null) {
return true;
}
ListNode walker = head;
ListNode runner = head;
while (runner != null && runner.next != null) {
walker = walker.next;
runner = runner.next.next;
}
ListNode newHead = reverse(walker);
while (newHead != null) {
if (head.val != newHead.val) {
return false;
}
head = head.next;
newHead = newHead.next;
}
return true;
}
private ListNode reverse(ListNode cur) {
ListNode pre = null;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
https://leetcode.com/discuss/44751/11-lines-12-with-restore-o-n-time-o-1-space
Reversed first half == Second half?
Phase 1: Reverse the first half while finding the middle.
Phase 2: Compare the reversed first half with the second half.
Same as the above, but while comparing the two halves, restore the list to its original state by reversing the first half back.Phase 2: Compare the reversed first half with the second half.
def isPalindrome(self, head): rev = None fast = head while fast and fast.next: fast = fast.next.next rev, rev.next, head = head, rev, head.next tail = head.next if fast else head isPali = True while rev: isPali = isPali and rev.val == tail.val head, head.next, rev = rev, head, rev.next tail = tail.next return isPali
https://github.com/shawnfan/LintCode/blob/master/Java/Palindrome%20Linked%20List.java
http://www.jiuzhang.com/solutions/palindrome-linked-list/
public boolean isPalindrome(ListNode head) { if (head == null) { return true; } ListNode middle = findMiddle(head); middle.next = reverse(middle.next); ListNode p1 = head, p2 = middle.next; while (p1 != null && p2 != null && p1.val == p2.val) { p1 = p1.next; p2 = p2.next; } return p2 == null; } private ListNode findMiddle(ListNode head) { if (head == null) { return null; } ListNode slow = head, fast = head.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } private ListNode reverse(ListNode head) { ListNode prev = null; while (head != null) { ListNode temp = head.next; head.next = prev; prev = head; head = temp; } return prev; }https://leetcode.com/discuss/50575/short-accepted-cpp-code
public boolean isPalindrome(ListNode head) { if(head==null) return true; ListNode fast = head; ListNode slow = head; ListNode pre=null; ListNode next = null; while(fast!=null&&fast.next!=null){ fast = fast.next.next;// 此步一定要在链表逆转操作之前执行 next = slow.next; slow.next = pre; pre = slow; slow = next; } if(fast!=null&&fast.next==null){ // 表示链表长度为奇数,则从中间节点的下一节点开始比较 slow = slow.next; } while(pre!=null&&slow!=null){ if(pre.val!=slow.val) return false; pre = pre.next; slow = slow.next; } return true; }
http://www.cnblogs.com/theskulls/p/4940695.html
再定义一个链表,存放链表反转的值,再以此比较两个链表中的值是否相等,时间复杂度O(N),空间复杂度O(N)
public boolean isPalindrome(ListNode head) { // Write your code here ArrayList<Integer> list = new ArrayList<Integer>(); if(head == null || head.next == null) return true; ListNode p = head; ListNode prev = new ListNode(head.val); while(p.next != null){ ListNode tmp = new ListNode(p.next.val); tmp.next = prev; prev = tmp; p = p.next; } ListNode p1 = head; ListNode p2 = prev; while(p1!=null){ if(p1.val != p2.val) return false; p1 = p1.next; p2 = p2.next; } return true; }
https://leetcode.com/discuss/52685/share-my-java-answer
Since it's a recursive algorithm, it uses O(N) space.
Every Node compared twice,add another flag to mark it can improve it.
public ListNode root;
public boolean isPalindrome(ListNode head) {
root = head;
return (head == null) ? true : _isPalindrome(head);
}
public boolean _isPalindrome(ListNode head) {
boolean flag = true;
if (head.next != null) {
flag = _isPalindrome(head.next);
}
if (flag && root.val == head.val) {
root = root.next;
return true;
}
return false;
}https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2002.%20Linked%20Lists/Q2_06_Palindrome
https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2002.%20Linked%20Lists/Q2_06_Palindrome/QuestionC.java
In order to apply this approach, we first need to know when we've reached the middle element, as this will form our base case. We can do this by passing in length - 2 for the length each time. When the length equals 0 or 1, we're at the center of the linked list. This is because the length is reduced by 2 each time. Once we've recursed Yi times, length will be down to 0.
public static class Result {public LinkedListNode node;
public boolean result;
public Result(LinkedListNode n, boolean res) {
node = n;
result = res;
}
}
public static Result isPalindromeRecurse(LinkedListNode head, int length) {
if (head == null || length <= 0) { // Even number of nodes
return new Result(head, true);
} else if (length == 1) { // Odd number of nodes
return new Result(head.next, true);
}
/* Recurse on sublist. */
Result res = isPalindromeRecurse(head.next, length - 2);
/* If child calls are not a palindrome, pass back up
* a failure. */
if (!res.result || res.node == null) {
return res;
}
/* Check if matches corresponding node on other side. */
res.result = (head.data == res.node.data);
/* Return corresponding node. */
res.node = res.node.next;
return res;
}
public static int lengthOfList(LinkedListNode n) {
int size = 0;
while (n != null) {
size++;
n = n.next;
}
return size;
}
public static boolean isPalindrome(LinkedListNode head) {
int length = lengthOfList(head);
Result p = isPalindromeRecurse(head, length);
return p.result;
}
https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2002.%20Linked%20Lists/Q2_06_Palindrome/QuestionB.java
public static boolean isPalindrome(LinkedListNode head) {
LinkedListNode fast = head;
LinkedListNode slow = head;
Stack<Integer> stack = new Stack<Integer>();
while (fast != null && fast.next != null) {
stack.push(slow.data);
slow = slow.next;
fast = fast.next.next;
}
/* Has odd number of elements, so skip the middle */
if (fast != null) {
slow = slow.next;
}
while (slow != null) {
int top = stack.pop().intValue();
if (top != slow.data) {
return false;
}
slow = slow.next;
}
return true;
}
https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2002.%20Linked%20Lists/Q2_06_Palindrome/QuestionA.java
public static boolean isPalindrome(LinkedListNode head) {
LinkedListNode reversed = reverseAndClone(head);
return isEqual(head, reversed);
}
public static LinkedListNode reverseAndClone(LinkedListNode node) {
LinkedListNode head = null;
while (node != null) {
LinkedListNode n = new LinkedListNode(node.data); // Clone
n.next = head;
head = n;
node = node.next;
}
return head;
}
public static boolean isEqual(LinkedListNode one, LinkedListNode two) {
while (one != null && two != null) {
if (one.data != two.data) {
return false;
}
one = one.next;
two = two.next;
}
return one == null && two == null;
}
https://leetcode.com/discuss/45309/reversing-a-list-is-not-considered-o-1-space
Read full article from Palindrome Linked List – Leetcode Java | Welkin Lan