leetcode 【 Merge Two Sorted Lists 】 python 实现

题目

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.

代码:oj在线测试通过 Runtime: 208 ms

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution:
 8     # @param two ListNodes
 9     # @return a ListNode
10     def mergeTwoLists(self, l1, l2):
11         if l1 is None:
12             return l2
13         if l2 is None:
14             return l1
15
16         dummyhead = ListNode(0)
17         dummyhead.next = None
18         p = dummyhead
19         while l1 is not None and l2 is not None:
20             if l1.val > l2.val:
21                 p.next = l2
22                 l2 = l2.next
23             else:
24                 p.next = l1
25                 l1 = l1.next
26             p = p.next
27         if l1 is not None:
28             p.next = l1
29         else:
30             p.next = l2
31         return dummyhead.next

思路

虚表头dummyhead设定好了就不要动,设定一个p=hummyhead,然后从处理p.next开始;最后返回hummyhead就可以获得正确的结果

两个表的指针一步步移动,并通过比较val的大小决定哪个插入p.next

注意,每次执行完判断,要移动指针p=p.next

时间: 2024-10-14 07:37:18

leetcode 【 Merge Two Sorted Lists 】 python 实现的相关文章

[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 Two Sorted Lists @ Python

原题地址:https://oj.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. 解题思路:合并两个已经排好序的链表. 代码: # Definition for sin

[leetcode]Merge k Sorted Lists @ Python [基础知识: heap]

原题地址: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个已经排好序的链表, 使用堆这一数据结构. 堆,也叫做:priority queue 首先将每条链表的头节点进入堆中. 然后将最小的弹出,并将最小的节点这条链

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: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-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x

[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: 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. Show Tags SOLUTION 1: 使用dummynode记录头节点的前一个,轻松完成,2分钟就AC啦! 1 /** 2 * Definition for sin

LeetCode: Merge k Sorted Lists 解题报告

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Show Tags 参考资料: http://blog.csdn.net/linhuanmars/article/details/19899259. SOLUTION 1: 使用分治法.左右分别递归调用Merge K sorted List,然后再使用merg

LeetCode -- Merge k Sorted Lists (Divide and Conquer / PriorityQueue)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 分支策略:每次归并两个已排好序的链表,直至只剩下一个链表. public class Solution { public ListNode mergeKLists(List<ListNode> lists) { //处理特殊情况  if (lists == null)             ret

21. Merge Two Sorted Lists —— Python

题目: 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. 没事来做做题,该题目是说两个排序好的链表组合起来,依然是排序好的,即链表的值从小到大. 代码: 于是乎,新建一个链表,next用两个链表当前位置去比较,谁的小就放谁.当一个链表放完之后,说明另外一个链表剩下的