您的位置:首页 > 其它

21. Merge Two Sorted Lists徒手尝试#2(Done)

2016-11-28 15:34 393 查看
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) {
ListNode dummy = new ListNode(0);
dummy.next = l1;
ListNode head = dummy;
while(l1 != null && l2 != null) {
if(l1.val < l2.val) {
head.next = l1;
l1 = l1.next;
} else {
head.next = l2;
l2 = l2.next;
}
head = head.next;
}

if(l1 == null) {
head.next = l2;
}
if(l2 == null) {
head.next = l1;
}
return dummy.next;
}
}


Problem#1

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