-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path21. Merge Two Sorted Lists.java
executable file
·60 lines (50 loc) · 1.38 KB
/
21. 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
E
tags: Linked List
time: O(n)
space: O(1)
如题
#### Basics
- 小的放前。每次比head大小
- while过后,把没完的list一口气接上。
- 一开始建一个node用来跑路, 每次都存node.next = xxx。存一个dummy。用来return dummy.next.
```
/*
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.
Example
Given 1->3->8->11->15->null, 2->null , return 1->2->3->8->11->15->null
Tags Expand
Linked List
Thinking process:
1. Merge sorted list, compare before add to node.next
2. when any of l1 or l2 is null, break out.
3. add the non-null list at the end of node.
4. return dummy.next.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode node = head;
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;
}
if (l1 != null) node.next = l1;;
if (l2 != null) node.next = l2;
return head.next;
}
}
```