Easy 21题 Merge Two Sorted Lists(必须重刷!!!)
2016-10-07 07:44
239 查看
QUESTION:
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.
SOLUTION
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
/* if (l1 == null) return l2;
if (l2 == null) return l1;
//we always get the small one
if (l1.val > l2.val)
{
ListNode tmp = l2;
tmp.next = mergeTwoLists(l1, l2.next);
return tmp;
}
else
{
ListNode tmp = l1;
tmp.next = mergeTwoLists(l1.next, l2);
return tmp;
}
*/
if (l1 == null || l2 == null) {
return l1 == null ? l2 : l1;
}
ListNode tmp=null;
ListNode head=null;
if(l1.val>l2.val)
{
head=l2;
l2=l2.next;
}
else
{
head=l1;
l1=l1.next;
}
tmp=head;
while(l1!=null&&l2!=null)
{
if(l1.val>l2.val)
{
tmp.next=l2;
l2=l2.next;
}
else
{
tmp.next=l1;
l1=l1.next;
}
tmp=tmp.next;
System.out.print("123");
}
if(l1==null)
{
tmp.next=l2;
}
else if(l2==null)
{
tmp.next=l1;
}
return head;
}
}
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.
SOLUTION
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
/* if (l1 == null) return l2;
if (l2 == null) return l1;
//we always get the small one
if (l1.val > l2.val)
{
ListNode tmp = l2;
tmp.next = mergeTwoLists(l1, l2.next);
return tmp;
}
else
{
ListNode tmp = l1;
tmp.next = mergeTwoLists(l1.next, l2);
return tmp;
}
*/
if (l1 == null || l2 == null) {
return l1 == null ? l2 : l1;
}
ListNode tmp=null;
ListNode head=null;
if(l1.val>l2.val)
{
head=l2;
l2=l2.next;
}
else
{
head=l1;
l1=l1.next;
}
tmp=head;
while(l1!=null&&l2!=null)
{
if(l1.val>l2.val)
{
tmp.next=l2;
l2=l2.next;
}
else
{
tmp.next=l1;
l1=l1.next;
}
tmp=tmp.next;
System.out.print("123");
}
if(l1==null)
{
tmp.next=l2;
}
else if(l2==null)
{
tmp.next=l1;
}
return head;
}
}
相关文章推荐
- [Leetcode 21, Easy] Merge Two Sorted Lists
- # 蜗牛慢慢爬 LeetCode 21. Merge Two Sorted Lists [Difficulty: Easy]
- LeetCode:21_Merge Two Sorted Lists | 合并两个排序列表 | Easy
- Easy 6 Merge Two Sorted Lists(21)
- LeetCode 21. Merge Two Sorted Lists (Easy)
- LeetCode-21-Merge Two Sorted Lists(链表/归并)-Easy
- 【Leetcode-Easy-21】Merge Two Sorted Lists
- Leetcode 21. Merge Two Sorted Lists (Easy) (cpp)
- Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]
- LeetCode 21 Merge Two Sorted Lists
- Leetcode 21 Merge Two Sorted Lists 将两个已排序的链表合并起来
- leetcode 21 Merge Two Sorted Lists
- leetcode 21. Merge Two Sorted Lists
- LeetCode 21 Merge Two Sorted Lists 把两个链表有序连接
- 21. Merge Two Sorted Lists
- leetcode 21 Merge Two Sorted Lists
- 21 Merge Two Sorted Lists(两链表归并排序Easy)
- 【Leetcode problrm 21】Merge Two Sorted Lists
- LeetCode 21:Merge Two Sorted Lists
- 21. Merge Two Sorted Lists