leetcode21 Merge Two Sorted Lists

 1 """
 2 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.
 3 Example:
 4 Input: 1->2->4, 1->3->4
 5 Output: 1->1->2->3->4->4
 6 """
 7 class ListNode:
 8     def __init__(self, x):
 9         self.val = x
10         self.next = None
11
12 class Solution:
13     def mergeTwoLists(self, l1, l2):
14         head = ListNode(0)
15         first = head         #!!!结果头指针
16         while l1 != None and l2 != None:
17             if l1.val <= l2.val:
18                 head.next = l1
19                 l1 = l1.next
20             else:
21                 head.next = l2
22                 l2 = l2.next
23             head = head.next #!!!跟随新链表增长的指针
24         if l1 == None:       #bug这个判别条件应该写在while之后
25             head.next = l2
26         elif l2 == None:
27             head.next = l1
28         return first.next

原文地址:https://www.cnblogs.com/yawenw/p/12273855.html

时间: 2024-11-09 00:05:33

leetcode21 Merge Two Sorted Lists的相关文章

LeetCode-21 Merge Two Sorted Lists Solution (with Java)

1. Description: 2. Examples: 3.Solutions: 1 /** 2 * Created by sheepcore on 2019-05-07 3 * Definition for singly-linked list. 4 * public class ListNode { 5 * int val; 6 * ListNode next; 7 * ListNode(int x) { val = x; } 8 * } 9 */ 10 class Solution {

LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode21. Merge Two Sorted Lists 示例: 输入: 1->2->4, 1->3->4 输出: 1->1->2->3->4->4 Java 实现 ListNode 类 class ListNode { int val; ListNode n

LeetCode 23: Merge K Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 本题在上一题(LeetCode 21: Merge Two Sorted Lists)基础上再加深了一步,链表个数从两个改为K个. 此题有如下几种解法: 1.最简单的方法莫过于每次都从K个链表头中找到最小的那个元素,加入结果链表中.基于此,有人通过最小堆来简化最小元素的比较. struct Compa

【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. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

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. 根据k个已经排好序的链表构造一个排序的链表,采用类似归并排序的算法可以通过测试 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;

[LeetCode]23. Merge k Sorted Lists

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定k个排序了的链表,合并k个链表成一个排序链表. 本程序思路: 1)首先得到K个链表的长度和存在len中 2)从K个链表中找到值最小的那个节点,把该节点添加到合并链表中 3)重复len次即可把所有节点添加到合并链表中. 注意事项: 1)K个链表中有的

Merge Two Sorted Lists &amp; Remove Nth Node From End of List

1.合并两个排好序的list 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. 2.删除list倒数第n个元素 Remove Nth Node From End of List Given a linked list,

[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][JAVA] Merge Two Sorted Lists &amp; Sort List

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. 两个排好序的链表拼接,只要用两个指针遍历链表即可. 可以借助一个helper指针来进行开头的合并.如果有一个遍历完,则直接把另一个链表指针之后的部分全部接在后面: 1