/
Solution.java
40 lines (36 loc) · 1.17 KB
/
Solution.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
/**
* Time : O(M + N); Space: O(1)
* @tag : Linked List
* @by : Steven Cooks
* @date: May 6, 2015
*************************************************************************
* Description:
*
* Merge two sorted linked lists and return it as a new list. The new list
* should be made by splicing together the nodes of the first two lists.
*
*************************************************************************
* {@link https://leetcode.com/problems/merge-two-sorted-lists/ }
*/
package _021_MergeTwoSortedLists;
import com.leetcode.ListNode;
/** see test {@link _021_MergeTwoSortedLists.SolutionTest } */
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode node = dummyHead;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
node.next = l1;
l1 = l1.next;
} else {
node.next = l2;
l2 = l2.next;
}
node = node.next;
}
// append the remaining list
node.next = (l1 != null) ? l1 : l2;
return dummyHead.next;
}
}