|
| 1 | +--- |
| 2 | +id: palindrome-linked-list |
| 3 | +title: Palindrome Linked List(LeetCode) |
| 4 | +sidebar_label: 0234-Palindrome Linked List |
| 5 | +tags: |
| 6 | + - Linked List |
| 7 | + - Two Pointer |
| 8 | + - Stack |
| 9 | + - Recursion |
| 10 | +description: Given the head of a singly linked list, return true if it is a palindrome or false otherwise. |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Statement |
| 14 | + |
| 15 | +Given the `head` of a singly linked list, return `true` if it is a palindrome or `false` otherwise. |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +```plaintext |
| 24 | +Input: head = [1,2,2,1] |
| 25 | +Output: true |
| 26 | +``` |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +```plaintext |
| 33 | +Input: head = [1,2] |
| 34 | +Output: false |
| 35 | +``` |
| 36 | + |
| 37 | +### Constraints |
| 38 | + |
| 39 | +- The number of nodes in the list is in the range `[1, 105]`. |
| 40 | +- `0 <= Node.val <= 9` |
| 41 | + |
| 42 | +## Solution |
| 43 | + |
| 44 | +### Approach |
| 45 | + |
| 46 | +To check if a linked list is a palindrome using O(1) extra space, we can reverse the second half of the linked list and then compare it to the first half. The key steps involve: |
| 47 | + |
| 48 | +1. Finding the middle of the linked list. |
| 49 | + |
| 50 | +  |
| 51 | + |
| 52 | +2. Reversing the second half of the list. |
| 53 | + |
| 54 | +  |
| 55 | + |
| 56 | +3. Comparing the two halves for equality. |
| 57 | + |
| 58 | +  |
| 59 | + |
| 60 | +(Note: This process works regardless of whether the length of the linked list is odd or even, as the comparison will stop when slow reaches the "dead-end" node.) |
| 61 | + |
| 62 | +  |
| 63 | + |
| 64 | +#### Algorithm |
| 65 | + |
| 66 | +1. Find the middle of the linked list: |
| 67 | +* Use two pointers, `slow` and `fast`. Move `slow` by one step and `fast` by two steps in each iteration. When `fast` reaches the end, slow will be at the middle. |
| 68 | +2. Reverse the second half: |
| 69 | +* Starting from the middle node, reverse the direction of the `next` pointers for each node until the end of the list. |
| 70 | +3. Compare the first and second halves: |
| 71 | +* Initialize two pointers, one at the head and the other at the start of the reversed second half. |
| 72 | +* Compare the values of the nodes pointed to by these pointers. If any values differ, the list is not a palindrome. If all values match, the list is a palindrome. |
| 73 | + |
| 74 | +#### Implementation |
| 75 | + |
| 76 | +Javascript Code: |
| 77 | + |
| 78 | +```Javascript |
| 79 | +var isPalindrome = function(head) { |
| 80 | + let slow = head, fast = head, prev = null, temp; |
| 81 | + while (fast && fast.next) { |
| 82 | + slow = slow.next; |
| 83 | + fast = fast.next.next; |
| 84 | + } |
| 85 | + prev = slow; |
| 86 | + slow = slow.next; |
| 87 | + prev.next = null; |
| 88 | + while (slow) { |
| 89 | + temp = slow.next; |
| 90 | + slow.next = prev; |
| 91 | + prev = slow; |
| 92 | + slow = temp; |
| 93 | + } |
| 94 | + fast = head; |
| 95 | + slow = prev; |
| 96 | + while (slow) { |
| 97 | + if (fast.val !== slow.val) return false; |
| 98 | + fast = fast.next; |
| 99 | + slow = slow.next; |
| 100 | + } |
| 101 | + return true; |
| 102 | +}; |
| 103 | +``` |
| 104 | + |
| 105 | +Python Code: |
| 106 | + |
| 107 | +```Python |
| 108 | +class Solution: |
| 109 | + def isPalindrome(self, head: ListNode) -> bool: |
| 110 | + slow, fast, prev = head, head, None |
| 111 | + while fast and fast.next: |
| 112 | + slow, fast = slow.next, fast.next.next |
| 113 | + prev, slow, prev.next = slow, slow.next, None |
| 114 | + while slow: |
| 115 | + slow.next, prev, slow = prev, slow, slow.next |
| 116 | + fast, slow = head, prev |
| 117 | + while slow: |
| 118 | + if fast.val != slow.val: |
| 119 | + return False |
| 120 | + fast, slow = fast.next, slow.next |
| 121 | + return True |
| 122 | +``` |
| 123 | + |
| 124 | +Java Code: |
| 125 | + |
| 126 | +```Java |
| 127 | +class Solution { |
| 128 | + public boolean isPalindrome(ListNode head) { |
| 129 | + ListNode slow = head, fast = head, prev = null, temp; |
| 130 | + while (fast != null && fast.next != null) { |
| 131 | + slow = slow.next; |
| 132 | + fast = fast.next.next; |
| 133 | + } |
| 134 | + prev = slow; |
| 135 | + slow = slow.next; |
| 136 | + prev.next = null; |
| 137 | + while (slow != null) { |
| 138 | + temp = slow.next; |
| 139 | + slow.next = prev; |
| 140 | + prev = slow; |
| 141 | + slow = temp; |
| 142 | + } |
| 143 | + fast = head; |
| 144 | + slow = prev; |
| 145 | + while (slow != null) { |
| 146 | + if (fast.val != slow.val) return false; |
| 147 | + fast = fast.next; |
| 148 | + slow = slow.next; |
| 149 | + } |
| 150 | + return true; |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +C++ Code: |
| 156 | + |
| 157 | +```C++ |
| 158 | +class Solution { |
| 159 | +public: |
| 160 | + bool isPalindrome(ListNode* head) { |
| 161 | + ListNode *slow = head, *fast = head, *prev = nullptr, *temp; |
| 162 | + while (fast && fast->next) { |
| 163 | + slow = slow->next; |
| 164 | + fast = fast->next->next; |
| 165 | + } |
| 166 | + prev = slow; |
| 167 | + slow = slow->next; |
| 168 | + prev->next = NULL; |
| 169 | + while (slow) { |
| 170 | + temp = slow->next; |
| 171 | + slow->next = prev; |
| 172 | + prev = slow; |
| 173 | + slow = temp; |
| 174 | + } |
| 175 | + fast = head; |
| 176 | + slow = prev; |
| 177 | + while (slow) { |
| 178 | + if (fast->val != slow->val) return false; |
| 179 | + fast = fast->next; |
| 180 | + slow = slow->next; |
| 181 | + } |
| 182 | + return true; |
| 183 | + } |
| 184 | +}; |
| 185 | +``` |
| 186 | +
|
| 187 | +### Complexity Analysis |
| 188 | +
|
| 189 | +- **Time complexity**: O(N) |
| 190 | +- **Space complexity**: O(1) |
| 191 | +
|
| 192 | +### Conclusion |
| 193 | +
|
| 194 | +By using two pointers to find the middle, reversing the second half of the linked list, and then comparing the two halves, we can determine if a linked list is a palindrome with a time complexity of O(N) and a space complexity of O(1). This approach ensures we do not use extra space beyond the input list itself, meeting the problem's constraints. |
0 commit comments