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用两个链表当前位置去比较,谁的小就放谁。当一个链表放完之后,说明另外一个链表剩下的元素都比较大,再放进去就好。

该题目简单,因为已经是两个排序好的链表了。

以下是Python代码,并有测试过程。

#coding:utf-8
# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if not l1 and not l2: return
        result = ListNode(0)
        l = result
        while l1 and l2:
            if l1.val < l2.val:
                l.next = l1
                l1 = l1.next
            else:
                l.next = l2
                l2 = l2.next
            #融合后链表的下一位,当前位置刚刚赋值
            l = l.next
        #把剩余的链表排在后面
        l.next = l1 or l2
        #返回融合后链表从第二个对象开始,第一个对象是自己创建的ListNode(0)
        return result.next

if __name__==‘__main__‘:
    #创建l1和l2两个链表,注意,排序好的就需要arr1和arr2中数字从小到大
    arr1 = [1,2,3]
    arr2 = [5,6,7]
    l1 = ListNode(arr1[0])
    p1 = l1
    l2 = ListNode(arr2[0])
    p2 = l2
    for i in arr1[1:]:
        p1.next = ListNode(i)
        p1 = p1.next
    for i in arr2[1:]:
        p2.next = ListNode(i)
        p2 = p2.next
    s=Solution()
    #融合两个链表
    q=s.mergeTwoLists(l1,l2)  
时间: 2024-08-08 09:41:21

21. 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 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 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

21. Merge Two Sorted Lists(js)

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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4题意

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description 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. Example Example: Input: 1->2->4, 1-&g

leedCode练题——21. Merge Two Sorted Lists(照搬大神做法)

1.题目 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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4-&g

[Leetcode][Python]21: Merge Two Sorted Lists

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 21: Merge Two Sorted Listshttps://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 th

C# 写 LeetCode easy #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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 代

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