forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoSortedList.java
More file actions
20 lines (19 loc) · 542 Bytes
/
MergeTwoSortedList.java
File metadata and controls
20 lines (19 loc) · 542 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class MergeTwoSortedList {
// 耗时15ms
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode p = l1, q = l2, cur = dummy;
for ( ; p != null && q != null; ) {
if (p.val < q.val) {
cur.next = p;
p = p.next;
} else {
cur.next = q;
q = q.next;
}
cur = cur.next;
}
cur.next = p != null ? p : q;
return dummy.next;
}
}