Skip to content

Commit 06371d5

Browse files
authored
Merge pull request #1264 from PradnyaGaitonde/PradnyaGaitonde-patch-6
Create 0234-palindrome-linked-list.md
2 parents 324eb4a + a61a189 commit 06371d5

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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+
![image](https://github.com/PradnyaGaitonde/codeharborhub.github.io/assets/116059908/3912ff1d-6425-4b30-aa4c-94955c9ee9bd)
22+
23+
```plaintext
24+
Input: head = [1,2,2,1]
25+
Output: true
26+
```
27+
28+
**Example 2:**
29+
30+
![image](https://github.com/PradnyaGaitonde/codeharborhub.github.io/assets/116059908/429d0cde-46de-4c2d-af7a-dd69bda5c426)
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+
![image](https://github.com/PradnyaGaitonde/codeharborhub.github.io/assets/116059908/5bfdbfd5-0f6f-4040-a946-9820621e2910)
51+
52+
2. Reversing the second half of the list.
53+
54+
![image](https://github.com/PradnyaGaitonde/codeharborhub.github.io/assets/116059908/5a2e1fdb-d8ca-4842-8638-23a1f0d3e2d5)
55+
56+
3. Comparing the two halves for equality.
57+
58+
![image](https://github.com/PradnyaGaitonde/codeharborhub.github.io/assets/116059908/ce99042e-62e4-4bf5-92ec-2fb01db9fc2d)
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+
![image](https://github.com/PradnyaGaitonde/codeharborhub.github.io/assets/116059908/7f423edf-044a-40f4-bccf-d762d65a66a9)
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

Comments
 (0)