Repository navigation
Expand file tree
/
Copy pathjointwolinkedlist.java
More file actions
55 lines (51 loc) · 1.59 KB
/
Copy pathjointwolinkedlist.java
File metadata and controls
55 lines (51 loc) · 1.59 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
package swordToOffer;
class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
public class jointwolinkedlist {
public ListNode Merge(ListNode list1, ListNode list2) {
if(list1 == null) return list2;
if(list2 == null) return list1;
ListNode newstart = null;
ListNode mark = null;
ListNode temp = null;
while (list1 != null && list2 != null) {
if(list1.val >= list2.val) {
temp = list2;
if(newstart == null) {
newstart = temp;
mark = newstart;
} else {
newstart.next = temp;
newstart = newstart.next;
}
list2 = list2.next;
} else {
temp = list1;
if(newstart == null) {
newstart = temp;
mark = newstart;
} else {
newstart.next = temp;
newstart = newstart.next;
}
list1 = list1.next;
}
}
while(list1 != null) {
temp.next = list1;
list1 = list1.next;
temp = temp.next;
}
while(list2 != null) {
temp.next = list2;
list2 = list2.next;
temp = temp.next;
}
return mark;
}
}
}