LeetCode 21. Merge Two Sorted Lists

原题链接在这里:https://leetcode.com/problems/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.

题解:

Method 1:

recursion, stop condition: 一条list 空了,返回另外一条。否则比较两个node value, 取小的,对应的点后移,next 指向用作下次比较返回的结果。

Time Complexity: O(m+n), m 是list1的长度,n 是list2 的长度.

Space: recursion 用的stack的大小, O(m+n).

Method 2:

Iteration, 生成一个dummy, 对应生成currentNode 用来iterate. 每次比较两个点,小的连在current 的后面直到 一个为 null, 再把没iterate完的那个连在current后面即可.

Time Complexity: O(m+n).

Space O(1). 

AC Java:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
14         /*
15         //Method 1, Recursion
16         if(l1 == null){
17             return l2;
18         }
19         if(l2 == null){
20             return l1;
21         }
22         
23         ListNode mergeHead;
24         if(l1.val <= l2.val){
25             mergeHead = l1;
26             mergeHead.next = mergeTwoLists(l1.next,l2);
27         }else{
28             mergeHead = l2;
29             mergeHead.next = mergeTwoLists(l1,l2.next);
30         }
31         
32         return mergeHead;
33         */
34         
35         //Method 2, Iteration
36         if(l1 == null){
37             return l2;
38         }
39         if(l2 == null){
40             return l1;
41         }
42         ListNode dummy = new ListNode(0);
43         ListNode current = dummy;
44         while(l1!=null && l2!= null){
45             if(l1.val<=l2.val){
46                 current.next = l1;
47                 l1 = l1.next;
48                 current = current.next;
49             }else{
50                 current.next = l2;
51                 l2 = l2.next;
52                 current = current.next;
53             }
54         }
55         if(l1 != null){
56             current.next = l1;
57         }
58         if(l2 != null){
59             current.next = l2;
60         }
61         return dummy.next;
62     }
63 }

Recursion C++.

AC C++:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode() : val(0), next(nullptr) {}
 7  *     ListNode(int x) : val(x), next(nullptr) {}
 8  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 9  * };
10  */
11 class Solution {
12 public:
13     ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14         if(list1 == NULL){
15             return list2;
16         }
17         
18         if(list2 == NULL){
19             return list1;
20         }
21         
22         if(list1 -> val <= list2 -> val){
23             list1 -> next = mergeTwoLists(list1 -> next, list2);
24             return list1;
25         }else{
26             list2 -> next = mergeTwoLists(list1, list2 -> next);
27             return list2;
28         }
29     }
30 };

Iteration C++.

AC C++:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode() : val(0), next(nullptr) {}
 7  *     ListNode(int x) : val(x), next(nullptr) {}
 8  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 9  * };
10  */
11 class Solution {
12 public:
13     ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14         if(!list1){
15             return list2;
16         }
17         
18         if(!list2){
19             return list1;
20         }
21         
22         ListNode* dummy = new ListNode(0);
23         ListNode* it = dummy;
24         while(list1 && list2){
25             if(list1->val <= list2->val){
26                 it->next = list1;
27                 list1 = list1->next;
28             }else{
29                 it->next = list2;
30                 list2 = list2->next;
31             }
32             
33             it = it->next;
34         }
35         
36         if(list1){
37             it->next = list1;
38         }
39         
40         if(list2){
41             it->next = list2;
42         }
43         
44         return dummy->next;
45     }
46 };

Recursion Python.

AC Python:

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, val=0, next=None):
 4 #         self.val = val
 5 #         self.next = next
 6 class Solution:
 7     def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
 8         if list1 is None:
 9             return list2
10         
11         if list2 is None:
12             return list1
13         
14         if list1.val < list2.val:
15             list1.next = self.mergeTwoLists(list1.next, list2)
16             return list1
17         else:
18             list2.next = self.mergeTwoLists(list1, list2.next)
19             return list2

Iteration Python.

AC Python:

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, val=0, next=None):
 4 #         self.val = val
 5 #         self.next = next
 6 class Solution:
 7     def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
 8         dummy = ListNode(-1)
 9         it = dummy
10         
11         while list1 and list2:
12             if list1.val <= list2.val:
13                 it.next = list1
14                 list1 = list1.next
15             else:
16                 it.next = list2
17                 list2 = list2.next
18             it = it.next
19         
20         if list1:
21             it.next = list1
22         if list2:
23             it.next = list2
24         
25         return dummy.next
26         

AC JavaScript:

 1 /**
 2  * Definition for singly-linked list.
 3  * function ListNode(val) {
 4  *     this.val = val;
 5  *     this.next = null;
 6  * }
 7  */
 8 /**
 9  * @param {ListNode} l1
10  * @param {ListNode} l2
11  * @return {ListNode}
12  */
13 var mergeTwoLists = function(l1, l2) {
14     var dummy = {
15         val : -1,
16         next : null
17     };
18     
19     var cur = dummy;
20     while(l1 && l2){
21         if(l1.val < l2.val){
22             cur.next = l1;
23             l1 = l1.next;
24         }else{
25             cur.next = l2;
26             l2 = l2.next;
27         }
28         
29         cur = cur.next;
30     }
31     
32     cur.next = l1 || l2;
33     return dummy.next;
34 };

跟上 Merge k Sorted ListsMerge Sorted ArraySort List.

posted @ 2015-10-14 09:04  Dylan_Java_NYC  阅读(231)  评论(0编辑  收藏  举报