Skip to content

Commit d56cf6a

Browse files
committed
07/29 linkedList problems
1 parent f3a6d7f commit d56cf6a

3 files changed

Lines changed: 81 additions & 51 deletions

File tree

java/mergeKLists.java

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
//Divide and Conquer method
22

3-
public class Solution {
3+
class Solution {
44
public ListNode mergeKLists(ListNode[] lists) {
5-
if (lists == null || lists.length == 0) return null;
6-
return sort(lists, 0, lists.length - 1);
5+
return merge(lists, 0, lists.length-1);
76
}
8-
9-
private ListNode sort(ListNode[] lists, int lo, int hi) {
10-
if (lo >= hi) return lists[lo];
11-
int mid = lo + (hi - lo) / 2;
12-
ListNode l1 = sort(lists, lo, mid);
13-
ListNode l2 = sort(lists, mid + 1, hi);
14-
return merge(l1, l2);
7+
8+
public ListNode merge(ListNode[] lists, int start, int end){
9+
if(start == end) return lists[start];
10+
else if(start < end){
11+
int mid = start + (end-start)/2;
12+
ListNode left = merge(lists, start, mid);
13+
ListNode right = merge(lists, mid+1, end);
14+
return mergeLists(left, right);
15+
}
16+
else
17+
return null;
1518
}
16-
17-
private ListNode merge(ListNode l1, ListNode l2) {
18-
if (l1 == null) return l2;
19-
if (l2 == null) return l1;
20-
if (l1.val < l2.val) {
21-
l1.next = merge(l1.next, l2);
19+
20+
public ListNode mergeLists(ListNode l1, ListNode l2){
21+
if(l1 == null) return l2;
22+
else if(l2 == null) return l1;
23+
else if(l1.val < l2.val){
24+
l1.next = mergeLists(l1.next, l2);
2225
return l1;
2326
}
24-
l2.next = merge(l1, l2.next);
25-
return l2;
27+
else {
28+
l2.next = mergeLists(l1, l2.next);
29+
return l2;
30+
}
2631
}
2732
}
2833

@@ -38,34 +43,34 @@ private ListNode merge(ListNode l1, ListNode l2) {
3843
*/
3944
class Solution {
4045
public ListNode mergeKLists(ListNode[] lists) {
41-
46+
4247
ListNode dummy = new ListNode(0), cur = dummy;
43-
48+
4449
if(lists.length == 0)
4550
return null;
46-
51+
4752
PriorityQueue<ListNode> minHeap = new PriorityQueue<>(lists.length, new Comparator<ListNode>(){
4853
public int compare(ListNode l1, ListNode l2)
4954
{
5055
return l1.val - l2.val;
5156
}
5257
});
53-
58+
5459
for(ListNode list : lists)
5560
if(list != null)
5661
minHeap.offer(list);
57-
62+
5863
while(!minHeap.isEmpty())
5964
{
6065
ListNode temp = minHeap.poll();
6166
cur.next = temp;
6267
if(temp.next != null)
63-
minHeap.offer(temp.next);
64-
68+
minHeap.offer(temp.next);
69+
6570
cur = temp;
6671
}
67-
68-
return dummy.next;
69-
72+
73+
return dummy.next;
74+
7075
}
7176
}

java/mergeTwoLists.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
1010
ListNode p1 = l1, p2 = l2;
1111
ListNode result = new ListNode(-1);
1212
ListNode p3 = result;
13-
13+
1414
while(p1 != null || p2 != null)
1515
{
1616
if(p1 == null && p2 != null)
@@ -29,16 +29,42 @@ else if(p1 != null && p2 == null)
2929
{
3030
p3.next = new ListNode(p1.val);
3131
p1 = p1.next;
32-
}
32+
}
3333
else
3434
{
3535
p3.next = new ListNode(p2.val);
3636
p2 = p2.next;
37-
}
37+
}
3838
}
39-
p3 = p3.next;
40-
39+
p3 = p3.next;
40+
4141
}
4242
return result.next;
4343
}
4444
}
45+
46+
47+
48+
49+
50+
51+
52+
/* better way to solve this */
53+
54+
55+
56+
class Solution {
57+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
58+
if(l1 == null) return l2;
59+
if(l2 == null) return l1;
60+
if(l1.val < l2.val){
61+
l1.next = mergeTwoLists(l1.next, l2);
62+
return l1;
63+
}
64+
else{
65+
l2.next = mergeTwoLists(l1, l2.next);
66+
return l2;
67+
}
68+
69+
}
70+
}

java/swapPairs.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@
66
* ListNode(int x) { val = x; }
77
* }
88
*/
9-
class Solution {
10-
public ListNode swapPairs(ListNode head) {
11-
ListNode first = new ListNode(-1);
12-
first.next = head;
13-
14-
ListNode pre = first, cur = head;
15-
16-
while(cur != null && cur.next != null)
17-
{
18-
pre.next = cur.next;
19-
pre = cur;
20-
cur = cur.next.next;
21-
pre.next.next = pre;
22-
}
23-
pre.next = cur;
24-
return first.next;
25-
}
26-
}
9+
class Solution {
10+
public ListNode swapPairs(ListNode head) {
11+
ListNode dummy = new ListNode(-1);
12+
dummy.next = head;
13+
ListNode cur = dummy;
14+
15+
while(cur.next != null && cur.next.next != null){
16+
ListNode first = cur.next;
17+
ListNode second = cur.next.next;
18+
first.next = second.next;
19+
second.next = cur.next;
20+
cur.next = second;
21+
cur = cur.next.next;
22+
}
23+
return dummy.next;
24+
}
25+
}

0 commit comments

Comments
 (0)