您的位置:首页 > 其它

leetcode 021 Merge Two Sorted Lists

2016-04-06 00:33 429 查看

题目

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 ListNode mergeTwoLists(ListNode l1, ListNode l2)
{
if(l1 == null)
return l2;
else if(l2 == null)
return l1;
ListNode p1 = l1, p2 = l2;
ListNode p3, l3;
p3 = l3 = new ListNode(0);//注意这里,设一个头节点,方便操作
l3.next = l1;
while(p1 != null && p2 != null)
{
if(p1.val <= p2.val)
{
p3.next = p1;
p3 = p1;
p1 = p1.next;
} else
{
p3.next = p2;
p3 = p2;
p2 = p2.next;
}
}
p3.next = p1==null ? p2 : p1;
return l3.next; //因为我们设了一个头节点,所以要跳过头节点
}


结果细节(图):

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: