# 21. Merge Two Sorted Lists

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.

/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode p1 = l1;
ListNode p2 = l2;
ListNode presult = new ListNode(0);
ListNode p = presult;

while(p1 != null && p2 != null){
if(p1.val >= p2.val){
p.next = p2;
p2 = p2.next;
}else{
p.next = p1;
p1 = p1.next;
}
p = p.next;
}
if(p1 != null){
p.next = p1;
}
if(p2 != null){
p.next = p2;
}

return presult.next;
}
}

#### 21. Merge Two Sorted Lists（python）

2016-09-18 21:32:17

#### [leetcode-排序]--21. Merge Two Sorted Lists

2017-02-05 16:53:38

#### 【leetcode c++】21 Merge Two Sorted Lists

2015-06-18 13:37:28

#### Leetcode 21. Merge Two Sorted Lists(C++)

2017-04-24 15:02:29

#### leetcode 21 Merge Two Sorted Lists

2015-06-05 21:59:59

#### [LeetCode] 021. Merge Two Sorted Lists (Easy) (C++/Python)

2015-03-04 20:11:32

#### LeetCode 21：Merge Two Sorted Lists

2015-05-27 23:08:48

#### LeetCode 21 Merge Two Sorted Lists（合并两个已排序的链表）（Linked List）

2015-11-10 20:38:04

#### leetcode第21题——*Merge Two Sorted Lists

2016-02-28 10:09:15

#### Merge Two Sorted Lists -- LeetCode

2014-02-22 23:31:00