您的位置:首页 > 其它

LeetCode笔记:21. Merge Two Sorted Lists

2016-09-28 15:23 357 查看

问题:

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.

大意:

合并两个有序链表并返回一个新链表。新链表应该包含原先两个链表中全部节点。

思路:

合并两个有序的链表其实思路还挺直接的,从两个链表的第一个节点开始比较大小,将小的那个放到新链表里,然后后移继续比较,大概思路就是这样,只是实现起来麻烦一点。另外其实还想到了一个奇葩的思路,就是将两个链表中的键值放到一个数组中,对数组进行排序,然后将数组里的元素按顺序放到新建的节点里去然后放入一个新链表就可以了哈哈哈。

代码(Java):

/**
* 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;
else if (l2 == null) return l1;

ListNode result;
ListNode mark;

if (l1.val <= l2.val) {
result = l1;
l1 = l1.next;
} else {
result = l2;
l2 = l2.next;
}
mark = result;

while (l1 != null) {
if (l2 != null && l2.val < l1.val) {
mark.next = l2;
mark = mark.next;
l2 = l2.next;
} else {
mark.next = l1;
mark = mark.next;
l1 = l1.next;
}
}
if (l2 != null) mark.next = l2;
return result;
}
}


他山之石:

public ListNode mergeTwoLists(ListNode l1, ListNode l2){
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val < l2.val){
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else{
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}


这个方法的思路其实差不多,但是用的是递归的方式。耗时1ms,而上面我的代码耗时16ms,真的有这么大的差异么。。。

合集:https://github.com/Cloudox/LeetCode-Record

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