-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMergeLinkedList.java
More file actions
41 lines (39 loc) · 906 Bytes
/
MergeLinkedList.java
File metadata and controls
41 lines (39 loc) · 906 Bytes
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
/**
* Definition for singly-linked list.
* class ListNode {
* public int val;
* public ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode A, ListNode B) {
ListNode newList = new ListNode(0);
ListNode dummy = newList;
while(true)
{
if(A == null)
{
dummy.next = B;
break;
}
if(B == null)
{
dummy.next = A;
break;
}
if(A.val > B.val)
{
dummy.next = B;
B = B.next;
}
else
{
dummy.next = A;
A = A.next;
}
dummy = dummy.next;
}
return newList.next;
}
}