Skip to content

Commit d206d5e

Browse files
committed
feat: update solutions to lc problems: No.0148,0234
1 parent e5854a1 commit d206d5e

File tree

8 files changed

+297
-51
lines changed

8 files changed

+297
-51
lines changed

solution/0100-0199/0148.Sort List/README.md

+98-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Solution {
118118
slow.next = null;
119119
ListNode l1 = sortList(head);
120120
ListNode l2 = sortList(t);
121-
ListNode dummy = new ListNode(0);
121+
ListNode dummy = new ListNode();
122122
ListNode cur = dummy;
123123
while (l1 != null && l2 != null) {
124124
if (l1.val <= l2.val) {
@@ -136,6 +136,103 @@ class Solution {
136136
}
137137
```
138138

139+
### **JavaScript**
140+
141+
```js
142+
/**
143+
* Definition for singly-linked list.
144+
* function ListNode(val, next) {
145+
* this.val = (val===undefined ? 0 : val)
146+
* this.next = (next===undefined ? null : next)
147+
* }
148+
*/
149+
/**
150+
* @param {ListNode} head
151+
* @return {ListNode}
152+
*/
153+
var sortList = function(head) {
154+
if (!head || !head.next) {
155+
return head;
156+
}
157+
let slow = head;
158+
let fast = head.next;
159+
while (fast && fast.next) {
160+
slow = slow.next;
161+
fast = fast.next.next;
162+
}
163+
let t = slow.next;
164+
slow.next = null;
165+
let l1 = sortList(head);
166+
let l2 = sortList(t);
167+
const dummy = new ListNode();
168+
let cur = dummy;
169+
while (l1 && l2) {
170+
if (l1.val <= l2.val) {
171+
cur.next = l1;
172+
l1 = l1.next;
173+
} else {
174+
cur.next = l2;
175+
l2 = l2.next;
176+
}
177+
cur = cur.next;
178+
}
179+
cur.next = l1 || l2;
180+
return dummy.next;
181+
};
182+
```
183+
184+
### **C#**
185+
186+
```cs
187+
/**
188+
* Definition for singly-linked list.
189+
* public class ListNode {
190+
* public int val;
191+
* public ListNode next;
192+
* public ListNode(int val=0, ListNode next=null) {
193+
* this.val = val;
194+
* this.next = next;
195+
* }
196+
* }
197+
*/
198+
public class Solution {
199+
public ListNode SortList(ListNode head) {
200+
if (head == null || head.next == null)
201+
{
202+
return head;
203+
}
204+
ListNode slow = head, fast = head.next;
205+
while (fast != null && fast.next != null)
206+
{
207+
slow = slow.next;
208+
fast = fast.next.next;
209+
}
210+
ListNode t = slow.next;
211+
slow.next = null;
212+
ListNode l1 = SortList(head);
213+
ListNode l2 = SortList(t);
214+
ListNode dummy = new ListNode();
215+
ListNode cur = dummy;
216+
while (l1 != null && l2 != null)
217+
{
218+
if (l1.val <= l2.val)
219+
{
220+
cur.next = l1;
221+
l1 = l1.next;
222+
}
223+
else
224+
{
225+
cur.next = l2;
226+
l2 = l2.next;
227+
}
228+
cur = cur.next;
229+
}
230+
cur.next = l1 == null ? l2 : l1;
231+
return dummy.next;
232+
}
233+
}
234+
```
235+
139236
### **...**
140237

141238
```

solution/0100-0199/0148.Sort List/README_EN.md

+98-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class Solution {
102102
slow.next = null;
103103
ListNode l1 = sortList(head);
104104
ListNode l2 = sortList(t);
105-
ListNode dummy = new ListNode(0);
105+
ListNode dummy = new ListNode();
106106
ListNode cur = dummy;
107107
while (l1 != null && l2 != null) {
108108
if (l1.val <= l2.val) {
@@ -120,6 +120,103 @@ class Solution {
120120
}
121121
```
122122

123+
### **JavaScript**
124+
125+
```js
126+
/**
127+
* Definition for singly-linked list.
128+
* function ListNode(val, next) {
129+
* this.val = (val===undefined ? 0 : val)
130+
* this.next = (next===undefined ? null : next)
131+
* }
132+
*/
133+
/**
134+
* @param {ListNode} head
135+
* @return {ListNode}
136+
*/
137+
var sortList = function(head) {
138+
if (!head || !head.next) {
139+
return head;
140+
}
141+
let slow = head;
142+
let fast = head.next;
143+
while (fast && fast.next) {
144+
slow = slow.next;
145+
fast = fast.next.next;
146+
}
147+
let t = slow.next;
148+
slow.next = null;
149+
let l1 = sortList(head);
150+
let l2 = sortList(t);
151+
const dummy = new ListNode();
152+
let cur = dummy;
153+
while (l1 && l2) {
154+
if (l1.val <= l2.val) {
155+
cur.next = l1;
156+
l1 = l1.next;
157+
} else {
158+
cur.next = l2;
159+
l2 = l2.next;
160+
}
161+
cur = cur.next;
162+
}
163+
cur.next = l1 || l2;
164+
return dummy.next;
165+
};
166+
```
167+
168+
### **C#**
169+
170+
```cs
171+
/**
172+
* Definition for singly-linked list.
173+
* public class ListNode {
174+
* public int val;
175+
* public ListNode next;
176+
* public ListNode(int val=0, ListNode next=null) {
177+
* this.val = val;
178+
* this.next = next;
179+
* }
180+
* }
181+
*/
182+
public class Solution {
183+
public ListNode SortList(ListNode head) {
184+
if (head == null || head.next == null)
185+
{
186+
return head;
187+
}
188+
ListNode slow = head, fast = head.next;
189+
while (fast != null && fast.next != null)
190+
{
191+
slow = slow.next;
192+
fast = fast.next.next;
193+
}
194+
ListNode t = slow.next;
195+
slow.next = null;
196+
ListNode l1 = SortList(head);
197+
ListNode l2 = SortList(t);
198+
ListNode dummy = new ListNode();
199+
ListNode cur = dummy;
200+
while (l1 != null && l2 != null)
201+
{
202+
if (l1.val <= l2.val)
203+
{
204+
cur.next = l1;
205+
l1 = l1.next;
206+
}
207+
else
208+
{
209+
cur.next = l2;
210+
l2 = l2.next;
211+
}
212+
cur = cur.next;
213+
}
214+
cur.next = l1 == null ? l2 : l1;
215+
return dummy.next;
216+
}
217+
}
218+
```
219+
123220
### **...**
124221

125222
```
+30-33
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
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 ListNode SortList(ListNode head) {
314
if (head == null || head.next == null)
415
{
516
return head;
617
}
7-
8-
ListNode p1 = null;
9-
var p2 = head;
10-
while (p2 != null)
18+
ListNode slow = head, fast = head.next;
19+
while (fast != null && fast.next != null)
1120
{
12-
p2 = p2.next;
13-
if (p2 != null)
14-
{
15-
p2 = p2.next;
16-
p1 = p1 == null ? head : p1.next;
17-
}
21+
slow = slow.next;
22+
fast = fast.next.next;
1823
}
19-
20-
p2 = p1.next;
21-
p1.next = null;
22-
p1 = head;
23-
p1 = SortList(p1);
24-
p2 = SortList(p2);
25-
ListNode newHead = null;
26-
ListNode newTail = null;
27-
while (p1 != null || p2 != null)
24+
ListNode t = slow.next;
25+
slow.next = null;
26+
ListNode l1 = SortList(head);
27+
ListNode l2 = SortList(t);
28+
ListNode dummy = new ListNode();
29+
ListNode cur = dummy;
30+
while (l1 != null && l2 != null)
2831
{
29-
if (p1 == null || (p2 != null && p1.val > p2.val))
30-
{
31-
var temp = p1;
32-
p1 = p2;
33-
p2 = temp;
34-
}
35-
var next = p1;
36-
p1 = p1.next;
37-
next.next = null;
38-
if (newTail == null)
32+
if (l1.val <= l2.val)
3933
{
40-
newHead = newTail = next;
34+
cur.next = l1;
35+
l1 = l1.next;
4136
}
4237
else
4338
{
44-
newTail.next = next;
45-
newTail = next;
39+
cur.next = l2;
40+
l2 = l2.next;
4641
}
42+
cur = cur.next;
4743
}
48-
return newHead;
44+
cur.next = l1 == null ? l2 : l1;
45+
return dummy.next;
4946
}
5047
}

solution/0100-0199/0148.Sort List/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public ListNode sortList(ListNode head) {
2222
slow.next = null;
2323
ListNode l1 = sortList(head);
2424
ListNode l2 = sortList(t);
25-
ListNode dummy = new ListNode(0);
25+
ListNode dummy = new ListNode();
2626
ListNode cur = dummy;
2727
while (l1 != null && l2 != null) {
2828
if (l1.val <= l2.val) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var sortList = function(head) {
13+
if (!head || !head.next) {
14+
return head;
15+
}
16+
let slow = head;
17+
let fast = head.next;
18+
while (fast && fast.next) {
19+
slow = slow.next;
20+
fast = fast.next.next;
21+
}
22+
let t = slow.next;
23+
slow.next = null;
24+
let l1 = sortList(head);
25+
let l2 = sortList(t);
26+
const dummy = new ListNode();
27+
let cur = dummy;
28+
while (l1 && l2) {
29+
if (l1.val <= l2.val) {
30+
cur.next = l1;
31+
l1 = l1.next;
32+
} else {
33+
cur.next = l2;
34+
l2 = l2.next;
35+
}
36+
cur = cur.next;
37+
}
38+
cur.next = l1 || l2;
39+
return dummy.next;
40+
};

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -167,26 +167,31 @@ var isPalindrome = function(head) {
167167
*/
168168
public class Solution {
169169
public bool IsPalindrome(ListNode head) {
170-
if (head == null || head.next == null) {
170+
if (head == null || head.next == null)
171+
{
171172
return true;
172173
}
173174
ListNode slow = head;
174175
ListNode fast = head.next;
175-
while (fast != null && fast.next != null) {
176+
while (fast != null && fast.next != null)
177+
{
176178
slow = slow.next;
177179
fast = fast.next.next;
178180
}
179181
ListNode cur = slow.next;
180182
slow.next = null;
181183
ListNode pre = null;
182-
while (cur != null) {
184+
while (cur != null)
185+
{
183186
ListNode t = cur.next;
184187
cur.next = pre;
185188
pre = cur;
186189
cur = t;
187190
}
188-
while (pre != null) {
189-
if (pre.val != head.val) {
191+
while (pre != null)
192+
{
193+
if (pre.val != head.val)
194+
{
190195
return false;
191196
}
192197
pre = pre.next;

0 commit comments

Comments
 (0)