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.其实题目理解起来很简单,就是讲两个有序的链表合并成一个新的链表。

 1 def mergeTwoLists(self, l1, l2):
 2         if l1 == None:
 3             return l2
 4         elif l2 == None:
 5             return l1
 6         elif l1 == None and l2 == None:
 7             return None
 8         else:
 9             p1 = l1
10             p2 = l2
11             if p1.val > p2.val:
12                 l = p2
13                 p = p2
14                 p2 = p2.next
15                 while p1 and p2:
16                     if p1.val > p2.val:
17                         p.next = p2
18                         p = p2
19                         p2 = p2.next
20
21                     else:
22                         p.next = p1
23                         p = p1
24                         p1 = p1.next
25
26                 if p1:
27                     while p1:
28                         p.next = p1
29                         p = p1
30                         p1 = p1.next
31                     return l
32                 if p2:
33                     while p2:
34                         p.next = p2
35                         p = p2
36                         p2 = p2.next
37                     return l
38             else:
39                 l = p1
40                 p = p1
41                 p1 = p1.next
42                 while p1 and p2:
43                     if p1.val > p2.val:
44                         p.next = p2
45                         p = p2
46                         p2 = p2.next
47
48                     else:
49                         p.next = p1
50                         p = p1
51                         p1 = p1.next
52
53                 if p1:
54                     while p1:
55                         p.next = p1
56                         p = p1
57                         p1 = p1.next
58                     return l
59                 if p2:
60                     while p2:
61                         p.next = p2
62                         p = p2
63                         p2 = p2.next
64                     return l
时间: 2024-10-01 03:27:57

Merge Two Sorted Lists,自己的代码一遍通过的相关文章

[LeetCode]148.Merge Two Sorted Lists

[题目] Sort a linked list in O(n log n) time using constant space complexity. [分析] 单链表适合用归并排序,双向链表适合用快速排序.本题可以复用Merge Two Sorted Lists方法 [代码] /********************************* * 日期:2015-01-12 * 作者:SJF0115 * 题目: 148.Merge Two Sorted Lists * 来源:https://

Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

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. 依次拼接 复杂度 时间 O(N) 空间 O(1) 思路 该题就是简单的把两个链表的节点拼接起来,我们可以用一个Dummy头,将比较过后的节点接在这个Dummy头之后.最后

LeetCode:Merge Two Sorted Lists - 拼接两个有序链表

1.题目名称 Merge Two Sorted Lists(按升序拼接两个有序链表) 2.题目地址 https://leetcode.com/problems/merge-two-sorted-lists/ 3.题目内容 英文: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 l

leetCode 21.Merge Two Sorted Lists (合并排序链表) 解题思路和方法

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. 思路:对两个已排序的单链表合并.算法上比较简单,与归并排序类似.只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力.而且想把重复代码写出方法,但是方法怎么都不

[leetcode]Merge k Sorted Lists @ Python

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路:归并k个已经排好序的链表.使用堆这一数据结构,首先将每条链表的头节点进入堆中,然后将最小的弹出,并将最小的节点这条链表的下一个节点入堆,依次类推,最终形成的链表就是归

[Leetcode] Merge k sorted lists 合并k个已排序的链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:这题最容易想到的是,(假设有k个链表)链表1.2合并,然后其结果12和3合并,以此类推,最后是123--k-1和k合并.至于两链表合并的过程见merge two sorted lists的分析.复杂度的分析见JustDoIT的博客.算法复杂度:假设每个链表的平均长度是n,则1.2合并,遍历2n个

leetCode 21. Merge Two Sorted Lists 合并链表

21. 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. 题目大意:合并两个有序的链表 思路:通过比较两个链表的节点大小,采用尾插法建立链表. 代码如下: /**  * Definition for singly-lin

LeetCode: Merge k Sorted Lists [022]

[题目] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [题意] 合并K个有序链表 [思路] 归并 [代码] /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), n

leetCode 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:此题是由合并两个排序链表演化而来,刚开始,想法比较简单,像求最大公共前缀一样,逐一求解:但是最后超时,所以马上意识到出题方是为了使用归并和分治的方法,故重新写了代码. 代码一(超时未过): /** * Definition for singly-link