-
Notifications
You must be signed in to change notification settings - Fork 15
/
merge-two-sorted-lists.java
45 lines (42 loc) · 1.15 KB
/
merge-two-sorted-lists.java
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
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode l1 is the head of the linked list
* @param ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
// write your code here
ListNode dummy = new ListNode(1);
ListNode runner = dummy;
while (l1 != null || l2 != null) {
if (l1 == null) {
runner.next = l2;
l2 = l2.next;
} else if (l2 == null) {
runner.next = l1;
l1 = l1.next;
} else {
if (l1.val < l2.val) {
runner.next = l1;
l1 = l1.next;
} else {
runner.next = l2;
l2 = l2.next;
}
}
runner = runner.next;
}
return dummy.next;
}
}