您的位置:首页 > 其它

Merge Two Sorted Lists

2015-07-19 09:22 330 查看
Problem: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 static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode p1=l1,p2=l2;
ListNode ll=new ListNode(0),p3=ll;
while(p1!=null&&p2!=null){
if(p1.val>=p2.val){
p3.next=p2;
p2=p2.next;
}else{
p3.next=p1;
p1=p1.next;
}
p3=p3.next;
}
if(p1!=null)
p3.next=p1;
if(p2!=null)
p3.next=p2;
return ll.next;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: