-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeTwoLists.java
More file actions
79 lines (68 loc) · 1.74 KB
/
Copy pathmergeTwoLists.java
File metadata and controls
79 lines (68 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Merge two sorted linked lists and return it as a new list.
<<<<<<< HEAD
The new list should be made by splicing together the nodes of the first two lists.
*/
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class mergeTwoLists {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
// seudo-head node: shead
ListNode shead = new ListNode(0);
shead.next = l1;
while (l1 != null && l2 != null) {
if(l1.val > l2.val) {
// if default order is ascending
shead.next = l2;
l2.next = l1;
} else {
ListNode
}
}
=======
The new list should be made by splicing together the nodes of the first two lists
*/
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class mergeTwoLists{
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
// boundary situation
if (l1 == null) return l2;
if (l2 == null) return l1;
// seudo head
ListNode shead = new ListNode(0);
// pointers for both lists and the head of new list
ListNode p1 = l1;
ListNode p2 = l2;
ListNode hp = shead;
// use seodo headpointer to stick to the smaller node sequencially
while(p1 != null && p2 != null) {
if (p1.val >= p2.val) {
hp.next = p2;
hp = hp.next;
p2 = p2.next;
} else {
hp.next = p1;
hp = hp.next;
p1 = p1.next;
}
}
// add on the last two nodes
if (p2 == null) {
hp.next = p1;
}
if (p1 == null) {
hp.next = p2;
}
return shead.next;
}
public static void main(String[] args) {
>>>>>>> 500c150fb3043c329bacdb78abbcc3b254ce50b8
}
}