Skip to content

Commit 5d5e1a6

Browse files
committed
feat: add solutions to lcci problem: No.02.06. Palindrome Linked List
1 parent 04fb43d commit 5d5e1a6

File tree

10 files changed

+417
-15
lines changed

10 files changed

+417
-15
lines changed

lcci/02.06.Palindrome Linked List/README.md

+144-4
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
```python
4242
# Definition for singly-linked list.
4343
# class ListNode:
44-
# def __init__(self, x):
45-
# self.val = x
46-
# self.next = None
44+
# def __init__(self, val=0, next=None):
45+
# self.val = val
46+
# self.next = next
4747
class Solution:
4848
def isPalindrome(self, head: ListNode) -> bool:
4949
if head is None or head.next is None:
@@ -73,7 +73,9 @@ class Solution:
7373
* public class ListNode {
7474
* int val;
7575
* ListNode next;
76-
* ListNode(int x) { val = x; }
76+
* ListNode() {}
77+
* ListNode(int val) { this.val = val; }
78+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
7779
* }
7880
*/
7981
class Solution {
@@ -108,6 +110,144 @@ class Solution {
108110
}
109111
```
110112

113+
### **JavaScript**
114+
115+
```js
116+
/**
117+
* Definition for singly-linked list.
118+
* function ListNode(val, next) {
119+
* this.val = (val===undefined ? 0 : val)
120+
* this.next = (next===undefined ? null : next)
121+
* }
122+
*/
123+
/**
124+
* @param {ListNode} head
125+
* @return {boolean}
126+
*/
127+
var isPalindrome = function(head) {
128+
if (!head || !head.next) {
129+
return true;
130+
}
131+
let slow = head;
132+
let fast = head.next;
133+
while (fast && fast.next) {
134+
slow = slow.next;
135+
fast = fast.next.next;
136+
}
137+
let cur = slow.next;
138+
slow.next = null;
139+
let pre = null;
140+
while (cur) {
141+
let t = cur.next;
142+
cur.next = pre;
143+
pre = cur;
144+
cur = t;
145+
}
146+
while (pre) {
147+
if (pre.val !== head.val) {
148+
return false;
149+
}
150+
pre = pre.next;
151+
head = head.next;
152+
}
153+
return true;
154+
};
155+
```
156+
157+
### **C#**
158+
159+
```cs
160+
/**
161+
* Definition for singly-linked list.
162+
* public class ListNode {
163+
* public int val;
164+
* public ListNode next;
165+
* public ListNode(int val=0, ListNode next=null) {
166+
* this.val = val;
167+
* this.next = next;
168+
* }
169+
* }
170+
*/
171+
public class Solution {
172+
public bool IsPalindrome(ListNode head) {
173+
if (head == null || head.next == null)
174+
{
175+
return true;
176+
}
177+
ListNode slow = head;
178+
ListNode fast = head.next;
179+
while (fast != null && fast.next != null)
180+
{
181+
slow = slow.next;
182+
fast = fast.next.next;
183+
}
184+
ListNode cur = slow.next;
185+
slow.next = null;
186+
ListNode pre = null;
187+
while (cur != null)
188+
{
189+
ListNode t = cur.next;
190+
cur.next = pre;
191+
pre = cur;
192+
cur = t;
193+
}
194+
while (pre != null)
195+
{
196+
if (pre.val != head.val)
197+
{
198+
return false;
199+
}
200+
pre = pre.next;
201+
head = head.next;
202+
}
203+
return true;
204+
}
205+
}
206+
```
207+
208+
### **TypeScript**
209+
210+
```ts
211+
/**
212+
* Definition for singly-linked list.
213+
* class ListNode {
214+
* val: number
215+
* next: ListNode | null
216+
* constructor(val?: number, next?: ListNode | null) {
217+
* this.val = (val===undefined ? 0 : val)
218+
* this.next = (next===undefined ? null : next)
219+
* }
220+
* }
221+
*/
222+
223+
function isPalindrome(head: ListNode | null): boolean {
224+
if (head == null || head.next == null) return true;
225+
// 快慢指针定位到中点
226+
let slow: ListNode = head, fast: ListNode = head.next;
227+
while (fast != null && fast.next != null) {
228+
slow = slow.next;
229+
fast = fast.next.next;
230+
}
231+
// 翻转链表
232+
let cur: ListNode = slow.next;
233+
slow.next = null;
234+
let prev: ListNode = null;
235+
while (cur != null) {
236+
let t: ListNode = cur.next;
237+
cur.next = prev;
238+
prev = cur;
239+
cur = t;
240+
}
241+
// 判断回文
242+
while (prev != null) {
243+
if (prev.val != head.val) return false;
244+
prev = prev.next;
245+
head = head.next;
246+
}
247+
return true;
248+
};
249+
```
250+
111251
### **...**
112252

113253
```

lcci/02.06.Palindrome Linked List/README_EN.md

+144-4
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ Could you do it in O(n) time and O(1) space?</p>
4343
```python
4444
# Definition for singly-linked list.
4545
# class ListNode:
46-
# def __init__(self, x):
47-
# self.val = x
48-
# self.next = None
46+
# def __init__(self, val=0, next=None):
47+
# self.val = val
48+
# self.next = next
4949
class Solution:
5050
def isPalindrome(self, head: ListNode) -> bool:
5151
if head is None or head.next is None:
@@ -73,7 +73,9 @@ class Solution:
7373
* public class ListNode {
7474
* int val;
7575
* ListNode next;
76-
* ListNode(int x) { val = x; }
76+
* ListNode() {}
77+
* ListNode(int val) { this.val = val; }
78+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
7779
* }
7880
*/
7981
class Solution {
@@ -108,6 +110,144 @@ class Solution {
108110
}
109111
```
110112

113+
### **JavaScript**
114+
115+
```js
116+
/**
117+
* Definition for singly-linked list.
118+
* function ListNode(val, next) {
119+
* this.val = (val===undefined ? 0 : val)
120+
* this.next = (next===undefined ? null : next)
121+
* }
122+
*/
123+
/**
124+
* @param {ListNode} head
125+
* @return {boolean}
126+
*/
127+
var isPalindrome = function(head) {
128+
if (!head || !head.next) {
129+
return true;
130+
}
131+
let slow = head;
132+
let fast = head.next;
133+
while (fast && fast.next) {
134+
slow = slow.next;
135+
fast = fast.next.next;
136+
}
137+
let cur = slow.next;
138+
slow.next = null;
139+
let pre = null;
140+
while (cur) {
141+
let t = cur.next;
142+
cur.next = pre;
143+
pre = cur;
144+
cur = t;
145+
}
146+
while (pre) {
147+
if (pre.val !== head.val) {
148+
return false;
149+
}
150+
pre = pre.next;
151+
head = head.next;
152+
}
153+
return true;
154+
};
155+
```
156+
157+
### **C#**
158+
159+
```cs
160+
/**
161+
* Definition for singly-linked list.
162+
* public class ListNode {
163+
* public int val;
164+
* public ListNode next;
165+
* public ListNode(int val=0, ListNode next=null) {
166+
* this.val = val;
167+
* this.next = next;
168+
* }
169+
* }
170+
*/
171+
public class Solution {
172+
public bool IsPalindrome(ListNode head) {
173+
if (head == null || head.next == null)
174+
{
175+
return true;
176+
}
177+
ListNode slow = head;
178+
ListNode fast = head.next;
179+
while (fast != null && fast.next != null)
180+
{
181+
slow = slow.next;
182+
fast = fast.next.next;
183+
}
184+
ListNode cur = slow.next;
185+
slow.next = null;
186+
ListNode pre = null;
187+
while (cur != null)
188+
{
189+
ListNode t = cur.next;
190+
cur.next = pre;
191+
pre = cur;
192+
cur = t;
193+
}
194+
while (pre != null)
195+
{
196+
if (pre.val != head.val)
197+
{
198+
return false;
199+
}
200+
pre = pre.next;
201+
head = head.next;
202+
}
203+
return true;
204+
}
205+
}
206+
```
207+
208+
### **TypeScript**
209+
210+
```ts
211+
/**
212+
* Definition for singly-linked list.
213+
* class ListNode {
214+
* val: number
215+
* next: ListNode | null
216+
* constructor(val?: number, next?: ListNode | null) {
217+
* this.val = (val===undefined ? 0 : val)
218+
* this.next = (next===undefined ? null : next)
219+
* }
220+
* }
221+
*/
222+
223+
function isPalindrome(head: ListNode | null): boolean {
224+
if (head == null || head.next == null) return true;
225+
// 快慢指针定位到中点
226+
let slow: ListNode = head, fast: ListNode = head.next;
227+
while (fast != null && fast.next != null) {
228+
slow = slow.next;
229+
fast = fast.next.next;
230+
}
231+
// 翻转链表
232+
let cur: ListNode = slow.next;
233+
slow.next = null;
234+
let prev: ListNode = null;
235+
while (cur != null) {
236+
let t: ListNode = cur.next;
237+
cur.next = prev;
238+
prev = cur;
239+
cur = t;
240+
}
241+
// 判断回文
242+
while (prev != null) {
243+
if (prev.val != head.val) return false;
244+
prev = prev.next;
245+
head = head.next;
246+
}
247+
return true;
248+
};
249+
```
250+
111251
### **...**
112252

113253
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public bool IsPalindrome(ListNode head) {
14+
if (head == null || head.next == null)
15+
{
16+
return true;
17+
}
18+
ListNode slow = head;
19+
ListNode fast = head.next;
20+
while (fast != null && fast.next != null)
21+
{
22+
slow = slow.next;
23+
fast = fast.next.next;
24+
}
25+
ListNode cur = slow.next;
26+
slow.next = null;
27+
ListNode pre = null;
28+
while (cur != null)
29+
{
30+
ListNode t = cur.next;
31+
cur.next = pre;
32+
pre = cur;
33+
cur = t;
34+
}
35+
while (pre != null)
36+
{
37+
if (pre.val != head.val)
38+
{
39+
return false;
40+
}
41+
pre = pre.next;
42+
head = head.next;
43+
}
44+
return true;
45+
}
46+
}

lcci/02.06.Palindrome Linked List/Solution.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode(int x) { val = x; }
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79
* }
810
*/
911
class Solution {

0 commit comments

Comments
 (0)