Skip to content

Commit b0bec36

Browse files
committed
[Function add]
1. Add leetcode solutions with tag list.
1 parent 411c90d commit b0bec36

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

leetcode/148. Sort List.md

+49-1
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,52 @@ class Solution {
115115
return dummy.next;
116116
}
117117
}
118-
```
118+
```
119+
120+
### Third Time
121+
* Method 1: Divide and conquer
122+
```Java
123+
/**
124+
* Definition for singly-linked list.
125+
* public class ListNode {
126+
* int val;
127+
* ListNode next;
128+
* ListNode(int x) { val = x; }
129+
* }
130+
*/
131+
class Solution {
132+
public ListNode sortList(ListNode head) {
133+
if(head == null || head.next == null) return head;
134+
ListNode dummy = new ListNode(0);
135+
dummy.next = head;
136+
ListNode slow = dummy, fast = dummy;
137+
while(fast != null && fast.next != null){
138+
slow = slow.next;
139+
fast = fast.next.next;
140+
}
141+
ListNode second = slow.next;
142+
slow.next = null;
143+
ListNode l1 = sortList(head);
144+
ListNode l2 = sortList(second);
145+
return merge(l1, l2);
146+
}
147+
private ListNode merge(ListNode l1, ListNode l2){
148+
ListNode dummy = new ListNode(0), cur = dummy;
149+
while(l1 != null || l2 != null){
150+
if(l1 != null && l2 != null){
151+
cur.next = l1.val <= l2.val ? l1: l2;
152+
if(l1.val <= l2.val) l1 = l1.next;
153+
else l2 = l2.next;
154+
}else if(l1 != null){
155+
cur.next = l1;
156+
break;
157+
}else{
158+
cur.next = l2;
159+
break;
160+
}
161+
cur = cur.next;
162+
}
163+
return dummy.next;
164+
}
165+
}
166+
```

leetcode/21. Merge Two Sorted Lists.md

+33
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,36 @@ class Solution {
7979
}
8080
}
8181
```
82+
83+
### Third Time
84+
* Method 1: Basic list question
85+
```Java
86+
/**
87+
* Definition for singly-linked list.
88+
* public class ListNode {
89+
* int val;
90+
* ListNode next;
91+
* ListNode(int x) { val = x; }
92+
* }
93+
*/
94+
class Solution {
95+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
96+
ListNode dummy = new ListNode(0), cur = dummy;
97+
while(l1 != null || l2 != null){
98+
if(l1 != null && l2 != null){
99+
cur.next = l1.val < l2.val ? l1: l2;
100+
if(l1.val < l2.val) l1 = l1.next;
101+
else l2 = l2.next;
102+
}else if(l1 != null){
103+
cur.next = l1;
104+
l1 = l1.next;
105+
}else{
106+
cur.next = l2;
107+
l2 = l2.next;
108+
}
109+
cur = cur.next;
110+
}
111+
return dummy.next;
112+
}
113+
}
114+
```

leetcode/23. Merge k Sorted Lists.md

+49-1
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,52 @@ class Solution {
128128
return dummy.next;
129129
}
130130
}
131-
```
131+
```
132+
133+
### Third Time
134+
* Method 1: Divide and conqur
135+
```Java
136+
/**
137+
* Definition for singly-linked list.
138+
* public class ListNode {
139+
* int val;
140+
* ListNode next;
141+
* ListNode(int x) { val = x; }
142+
* }
143+
*/
144+
class Solution {
145+
private ListNode[] l;
146+
public ListNode mergeKLists(ListNode[] lists) {
147+
if(lists.length == 0) return null;
148+
else if(lists.length == 1) return lists[0];
149+
this.l = lists;
150+
return sort(0, this.l.length - 1);
151+
}
152+
private ListNode sort(int low, int high){
153+
if(low >= high) return l[low];
154+
int mid = low + (high - low) / 2;
155+
ListNode l1 = sort(low, mid);
156+
ListNode l2 = sort(mid + 1, high);
157+
return merge(l1, l2);
158+
}
159+
private ListNode merge(ListNode l1, ListNode l2){
160+
ListNode dummy = new ListNode(0), cur = dummy;
161+
ListNode first = l1, second = l2;
162+
while(first != null || second != null){
163+
if(first != null && second != null){
164+
cur.next = first.val < second.val ? first: second;
165+
if(first.val < second.val) first = first.next;
166+
else second = second.next;
167+
}else if(first != null){
168+
cur.next = first;
169+
break;
170+
}else{
171+
cur.next = second;
172+
break;
173+
}
174+
cur = cur.next;
175+
}
176+
return dummy.next;
177+
}
178+
}
179+
```

0 commit comments

Comments
 (0)