合并两个有序链表(Python and C++解法)

题目:

  将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

示例:

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists

思路:

  注意输入空链表时的特殊处理;

  使用两个指针分别指向两个链表;

  合并过程中,每次合并的步骤都和之前的相同,因此可以采用递归解法,递归解法的思路需要注意。

Python解法:

 1 class ListNode(object):
 2     def __init__(self, x):
 3         self.val = x
 4         self.next = None
 5 
 6 
 7 class Solution(object):
 8     def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
 9         if l1 is None:
10             return l2
11         elif l2 is None:
12             return l1
13         prehead = ListNode(0)  # 定义一个不存储任何数据的哨兵节点,便于返回结果
14         prev = prehead
15         while l1 and l2:
16             if l1.val < l2.val:
17                 prev.next = l1
18                 l1 = l1.next
19             else:
20                 prev.next = l2
21                 l2 = l2.next
22             prev = prev.next
23         if l1 is None:
24             prev.next = l2
25         if l2 is None:
26             prev.next = l1
27         return prehead.next
28 
29 
30 if __name__ == '__main__':
31     node_11 = ListNode(1)
32     node_12 = ListNode(3)
33     node_13 = ListNode(4)
34     node_11.next = node_12
35     node_12.next = node_13
36 
37     node_21 = ListNode(0)
38     node_22 = ListNode(2)
39     node_23 = ListNode(5)
40     node_21.next = node_22
41     node_22.next = node_23
42 
43     s = Solution()
44     newNode = s.mergeTwoLists(node_11, node_21)
45     while newNode.next is not None:
46         print(newNode.val)
47         newNode = newNode.next

C++递归解法:

 1 struct ListNode {
 2     int val;
 3     ListNode *next;
 4     ListNode(int x) : val(x), next(NULL) {}
 5 };
 6 
 7 class Solution
 8 {
 9 public:
10     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
11         if (l1 == NULL)
12             return l2;
13         if (l2 == NULL)
14             return l1;
15         ListNode *tempHead = new ListNode(-1);
16 
17         if (l1->val < l2->val) {
18             tempHead = l1;
19             tempHead->next = mergeTwoLists(l1->next, l2);
20         }
21         else {
22             tempHead = l2;
23             tempHead->next = mergeTwoLists(l1, l2->next);
24         }
25         return tempHead;  // tempHead会一级一级的返回
26     }
27 };
28 
29 int main() {
30     ListNode *node_11 = new ListNode(1);
31     ListNode *node_12 = new ListNode(3);
32     ListNode *node_13 = new ListNode(5);
33     node_11 -> next = node_12;
34     node_12->next = node_13;
35 
36     ListNode *node_21 = new ListNode(2);
37     ListNode *node_22 = new ListNode(4);
38     ListNode *node_23 = new ListNode(4);
39     node_21->next = node_22;
40     node_22->next = node_23;
41 
42     Solution s;
43     ListNode *newNode = s.mergeTwoLists(node_11, node_21);
44     while (newNode != NULL) {
45         cout << newNode->val << " ";
46         newNode = newNode->next;
47     }
posted @ 2020-07-06 15:53  孔子?孟子?小柱子!  阅读(204)  评论(0编辑  收藏  举报