leetcode234 - Palindrome Linked List - easy
Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2
Output: false
Example 2:
Input: 1->2->2->1
Output: true
Follow up:
Could you do it in O(n) time and O(1) space?
1.stack。O(n), O(n)
快慢指针找出中点,在找中点的过程中把前半段压入栈。接下来对比中点后半段的内容和stack pop出来的内容是否一致,出现不一致就是false,遍历完为true。
2.半段链表反转。O(n), O(1)。for follow up。
快慢指针找出中点,把中点后半段链表反转,接着从head和中点开始并行往前,一个个对比。
实现1:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public boolean isPalindrome(ListNode head) { Stack<Integer> stack = new Stack<>(); ListNode fast = head, slow = head; // 1-2-3-4 32 null3 // 1-2-3 32 while (fast != null && fast.next != null) { fast = fast.next.next; stack.push(slow.val); slow = slow.next; } if (fast != null) { slow = slow.next; } while (!stack.isEmpty()) { if (stack.peek() != slow.val) { return false; } stack.pop(); slow = slow.next; } return true; } }
实现2:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public boolean isPalindrome(ListNode head) { ListNode mid = findMiddle(head); mid = reverseList(mid); while (head != null && mid != null) { if (head.val != mid.val) { return false; } head = head.next; mid = mid.next; } return true; } private ListNode findMiddle(ListNode head) { ListNode fast = head, slow = head; // 1-2-3-4 32 null3 // 1-2-3 32 while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; } // return the first node after the midpoint. 1234 -> 3, 12345 -> 4 if (fast != null) { slow = slow.next; } return slow; } private ListNode reverseList(ListNode head) { ListNode prev = null; while (head != null) { ListNode temp = head.next; head.next = prev; prev = head; head = temp; } return prev; } }