Skip to content

Commit f87c8d9

Browse files
committed
feat: update solutions to lc problem: No.0147. Insertion Sort List
1 parent 54dcb0f commit f87c8d9

File tree

5 files changed

+25
-30
lines changed

5 files changed

+25
-30
lines changed

solution/0100-0199/0147.Insertion Sort List/README.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
<strong>输出:</strong> -1-&gt;0-&gt;3-&gt;4-&gt;5
3737
</pre>
3838

39-
4039
## 解法
4140

4241
<!-- 这里可写通用的实现逻辑 -->
@@ -57,16 +56,14 @@
5756
```python
5857
# Definition for singly-linked list.
5958
# class ListNode:
60-
# def __init__(self, x):
61-
# self.val = x
62-
# self.next = None
63-
59+
# def __init__(self, val=0, next=None):
60+
# self.val = val
61+
# self.next = next
6462
class Solution:
6563
def insertionSortList(self, head: ListNode) -> ListNode:
6664
if head is None or head.next is None:
6765
return head
68-
dummy = ListNode(head.val)
69-
dummy.next = head
66+
dummy = ListNode(head.val, head)
7067
pre, cur = dummy, head
7168
while cur:
7269
if pre.val <= cur.val:
@@ -93,16 +90,17 @@ class Solution:
9390
* public class ListNode {
9491
* int val;
9592
* ListNode next;
96-
* ListNode(int x) { val = x; }
93+
* ListNode() {}
94+
* ListNode(int val) { this.val = val; }
95+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9796
* }
9897
*/
9998
class Solution {
10099
public ListNode insertionSortList(ListNode head) {
101100
if (head == null || head.next == null) {
102101
return head;
103102
}
104-
ListNode dummy = new ListNode(head.val);
105-
dummy.next = head;
103+
ListNode dummy = new ListNode(head.val, head);
106104
ListNode pre = dummy, cur = head;
107105
while (cur != null) {
108106
if (pre.val <= cur.val) {

solution/0100-0199/0147.Insertion Sort List/README_EN.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
<li><code>-5000 &lt;= Node.val &lt;= 5000</code></li>
4040
</ul>
4141

42-
4342
## Solutions
4443

4544
<!-- tabs:start -->
@@ -49,16 +48,14 @@
4948
```python
5049
# Definition for singly-linked list.
5150
# class ListNode:
52-
# def __init__(self, x):
53-
# self.val = x
54-
# self.next = None
55-
51+
# def __init__(self, val=0, next=None):
52+
# self.val = val
53+
# self.next = next
5654
class Solution:
5755
def insertionSortList(self, head: ListNode) -> ListNode:
5856
if head is None or head.next is None:
5957
return head
60-
dummy = ListNode(head.val)
61-
dummy.next = head
58+
dummy = ListNode(head.val, head)
6259
pre, cur = dummy, head
6360
while cur:
6461
if pre.val <= cur.val:
@@ -83,16 +80,17 @@ class Solution:
8380
* public class ListNode {
8481
* int val;
8582
* ListNode next;
86-
* ListNode(int x) { val = x; }
83+
* ListNode() {}
84+
* ListNode(int val) { this.val = val; }
85+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
8786
* }
8887
*/
8988
class Solution {
9089
public ListNode insertionSortList(ListNode head) {
9190
if (head == null || head.next == null) {
9291
return head;
9392
}
94-
ListNode dummy = new ListNode(head.val);
95-
dummy.next = head;
93+
ListNode dummy = new ListNode(head.val, head);
9694
ListNode pre = dummy, cur = head;
9795
while (cur != null) {
9896
if (pre.val <= cur.val) {

solution/0100-0199/0147.Insertion Sort List/Solution.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
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 {
1012
public ListNode insertionSortList(ListNode head) {
1113
if (head == null || head.next == null) {
1214
return head;
1315
}
14-
ListNode dummy = new ListNode(head.val);
15-
dummy.next = head;
16+
ListNode dummy = new ListNode(head.val, head);
1617
ListNode pre = dummy, cur = head;
1718
while (cur != null) {
1819
if (pre.val <= cur.val) {
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# Definition for singly-linked list.
22
# class ListNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.next = None
6-
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
76
class Solution:
87
def insertionSortList(self, head: ListNode) -> ListNode:
98
if head is None or head.next is None:
109
return head
11-
dummy = ListNode(head.val)
12-
dummy.next = head
10+
dummy = ListNode(head.val, head)
1311
pre, cur = dummy, head
1412
while cur:
1513
if pre.val <= cur.val:
@@ -23,4 +21,4 @@ def insertionSortList(self, head: ListNode) -> ListNode:
2321
p.next = cur
2422
pre.next = t
2523
cur = t
26-
return dummy.next
24+
return dummy.next
Loading

0 commit comments

Comments
 (0)