# 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) {
while(l1 != null && l2 != null){
ListNode nextNode = null;
if(l1.val > l2.val){
nextNode = l2;
l2 = l2.next;
current.next = nextNode;
current = nextNode;
}
else if(l2.val >= l1.val){
nextNode = l1;
l1 = l1.next;
current.next = nextNode;
current = nextNode;
}
}

if(l1 != null){
current.next = l1;
}
else if (l2 != null){
current.next = l2;
}

}
}

• 本文已收录于以下专栏：

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

[LeetCode] 021. Merge Two Sorted Lists (Easy) (C++/Python)
• hcbbt
• 2015年03月04日 20:11
• 3441

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

Question 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The...

## LeetCode：Merge Two Sorted Lists

Merge Two Sorted Lists1、题目： Merge two sorted linked lists and return it as a new list. The new list...
• BestZem
• 2016年07月11日 17:16
• 131

## LeetCode|Merge Two Sorted Lists

【问题描述】 Merge two sorted linked lists and return it as a new list. The new list should be made by sp...

## LeetCode 之 Merge Two / k Sorted Lists — C/C++ 实现

Merge Two Sorted Lists   Merge two sorted linked lists and return it as a new list. The new list sh...

举报原因： 您举报文章：Merge Two Sorted Lists 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)