Skip to content

Commit 5fd8253

Browse files
committed
feat: update solutions to lc problem: No.0234. Palindrome Linked List
1 parent d28db5f commit 5fd8253

File tree

6 files changed

+266
-106
lines changed

6 files changed

+266
-106
lines changed

solution/0200-0299/0234.Palindrome Linked List/README.md

+96-4
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
```python
3939
# Definition for singly-linked list.
4040
# class ListNode:
41-
# def __init__(self, x):
42-
# self.val = x
43-
# self.next = None
41+
# def __init__(self, val=0, next=None):
42+
# self.val = val
43+
# self.next = next
4444
class Solution:
4545
def isPalindrome(self, head: ListNode) -> bool:
4646
if head is None or head.next is None:
@@ -70,7 +70,9 @@ class Solution:
7070
* public class ListNode {
7171
* int val;
7272
* ListNode next;
73-
* ListNode(int x) { val = x; }
73+
* ListNode() {}
74+
* ListNode(int val) { this.val = val; }
75+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
7476
* }
7577
*/
7678
class Solution {
@@ -105,6 +107,96 @@ class Solution {
105107
}
106108
```
107109

110+
### **JavaScript**
111+
112+
```js
113+
/**
114+
* Definition for singly-linked list.
115+
* function ListNode(val, next) {
116+
* this.val = (val===undefined ? 0 : val)
117+
* this.next = (next===undefined ? null : next)
118+
* }
119+
*/
120+
/**
121+
* @param {ListNode} head
122+
* @return {boolean}
123+
*/
124+
var isPalindrome = function(head) {
125+
if (!head || !head.next) {
126+
return true;
127+
}
128+
let slow = head;
129+
let fast = head.next;
130+
while (fast && fast.next) {
131+
slow = slow.next;
132+
fast = fast.next.next;
133+
}
134+
let cur = slow.next;
135+
slow.next = null;
136+
let pre = null;
137+
while (cur) {
138+
let t = cur.next;
139+
cur.next = pre;
140+
pre = cur;
141+
cur = t;
142+
}
143+
while (pre) {
144+
if (pre.val !== head.val) {
145+
return false;
146+
}
147+
pre = pre.next;
148+
head = head.next;
149+
}
150+
return true;
151+
};
152+
```
153+
154+
### **C#**
155+
156+
```cs
157+
/**
158+
* Definition for singly-linked list.
159+
* public class ListNode {
160+
* public int val;
161+
* public ListNode next;
162+
* public ListNode(int val=0, ListNode next=null) {
163+
* this.val = val;
164+
* this.next = next;
165+
* }
166+
* }
167+
*/
168+
public class Solution {
169+
public bool IsPalindrome(ListNode head) {
170+
if (head == null || head.next == null) {
171+
return true;
172+
}
173+
ListNode slow = head;
174+
ListNode fast = head.next;
175+
while (fast != null && fast.next != null) {
176+
slow = slow.next;
177+
fast = fast.next.next;
178+
}
179+
ListNode cur = slow.next;
180+
slow.next = null;
181+
ListNode pre = null;
182+
while (cur != null) {
183+
ListNode t = cur.next;
184+
cur.next = pre;
185+
pre = cur;
186+
cur = t;
187+
}
188+
while (pre != null) {
189+
if (pre.val != head.val) {
190+
return false;
191+
}
192+
pre = pre.next;
193+
head = head.next;
194+
}
195+
return true;
196+
}
197+
}
198+
```
199+
108200
### **...**
109201

110202
```

solution/0200-0299/0234.Palindrome Linked List/README_EN.md

+96-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:
@@ -71,7 +71,9 @@ class Solution:
7171
* public class ListNode {
7272
* int val;
7373
* ListNode next;
74-
* ListNode(int x) { val = x; }
74+
* ListNode() {}
75+
* ListNode(int val) { this.val = val; }
76+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
7577
* }
7678
*/
7779
class Solution {
@@ -106,6 +108,96 @@ class Solution {
106108
}
107109
```
108110

111+
### **JavaScript**
112+
113+
```js
114+
/**
115+
* Definition for singly-linked list.
116+
* function ListNode(val, next) {
117+
* this.val = (val===undefined ? 0 : val)
118+
* this.next = (next===undefined ? null : next)
119+
* }
120+
*/
121+
/**
122+
* @param {ListNode} head
123+
* @return {boolean}
124+
*/
125+
var isPalindrome = function(head) {
126+
if (!head || !head.next) {
127+
return true;
128+
}
129+
let slow = head;
130+
let fast = head.next;
131+
while (fast && fast.next) {
132+
slow = slow.next;
133+
fast = fast.next.next;
134+
}
135+
let cur = slow.next;
136+
slow.next = null;
137+
let pre = null;
138+
while (cur) {
139+
let t = cur.next;
140+
cur.next = pre;
141+
pre = cur;
142+
cur = t;
143+
}
144+
while (pre) {
145+
if (pre.val !== head.val) {
146+
return false;
147+
}
148+
pre = pre.next;
149+
head = head.next;
150+
}
151+
return true;
152+
};
153+
```
154+
155+
### **C#**
156+
157+
```cs
158+
/**
159+
* Definition for singly-linked list.
160+
* public class ListNode {
161+
* public int val;
162+
* public ListNode next;
163+
* public ListNode(int val=0, ListNode next=null) {
164+
* this.val = val;
165+
* this.next = next;
166+
* }
167+
* }
168+
*/
169+
public class Solution {
170+
public bool IsPalindrome(ListNode head) {
171+
if (head == null || head.next == null) {
172+
return true;
173+
}
174+
ListNode slow = head;
175+
ListNode fast = head.next;
176+
while (fast != null && fast.next != null) {
177+
slow = slow.next;
178+
fast = fast.next.next;
179+
}
180+
ListNode cur = slow.next;
181+
slow.next = null;
182+
ListNode pre = null;
183+
while (cur != null) {
184+
ListNode t = cur.next;
185+
cur.next = pre;
186+
pre = cur;
187+
cur = t;
188+
}
189+
while (pre != null) {
190+
if (pre.val != head.val) {
191+
return false;
192+
}
193+
pre = pre.next;
194+
head = head.next;
195+
}
196+
return true;
197+
}
198+
}
199+
```
200+
109201
### **...**
110202

111203
```
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,41 @@
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+
*/
112
public class Solution {
213
public bool IsPalindrome(ListNode head) {
3-
if (head == null) return true;
4-
var count = Count(head);
5-
var temp = head;
6-
var c = 1;
7-
while (c < count / 2)
8-
{
9-
++c;
10-
temp = temp.next;
14+
if (head == null || head.next == null) {
15+
return true;
1116
}
12-
var head2 = Reverse(temp.next);
13-
temp.next = null;
14-
15-
while (head != null && head2 != null)
16-
{
17-
if (head.val != head2.val) return false;
18-
head = head.next;
19-
head2 = head2.next;
17+
ListNode slow = head;
18+
ListNode fast = head.next;
19+
while (fast != null && fast.next != null) {
20+
slow = slow.next;
21+
fast = fast.next.next;
2022
}
21-
return true;
22-
}
23-
24-
private int Count(ListNode head)
25-
{
26-
var count = 0;
27-
while (head != null)
28-
{
29-
++count;
30-
head = head.next;
23+
ListNode cur = slow.next;
24+
slow.next = null;
25+
ListNode pre = null;
26+
while (cur != null) {
27+
ListNode t = cur.next;
28+
cur.next = pre;
29+
pre = cur;
30+
cur = t;
3131
}
32-
return count;
33-
}
34-
35-
private ListNode Reverse(ListNode head)
36-
{
37-
var temp = head;
38-
head = null;
39-
while (temp != null)
40-
{
41-
var temp2 = temp.next;
42-
temp.next = head;
43-
head = temp;
44-
temp = temp2;
32+
while (pre != null) {
33+
if (pre.val != head.val) {
34+
return false;
35+
}
36+
pre = pre.next;
37+
head = head.next;
4538
}
46-
return head;
39+
return true;
4740
}
4841
}

solution/0200-0299/0234.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)