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->4

2、我的解答

# -*- coding: utf-8 -*-# @Time    : 2020/2/1 18:30# @Author  : SmartCat0929# @Email   : [email protected]# @Link    : https://github.com/SmartCat0929# @Site    : # @File    : 21.Merge Two Sorted Lists.py

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

class Solution:    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:        head = l3 = ListNode(0)        while(l1 is not None and l2 is not None):            if l1.val <=l2.val:                l3.next = ListNode(l1.val)                l3 = l3.next                l1 = l1.next            else:                l3.next = ListNode(l2.val)                l3 = l3.next                l2 = l2.next        while l1:            l3.next = ListNode(l1.val)            l3 = l3.next            l1 = l1.next        while l2:            l3.next = ListNode(l2.val)            l3 = l3.next            l2 = l2.next        return head.next

h1 = ListNode(1)h1.next = ListNode(2)h1.next.next = ListNode(4)

h2 = ListNode(1)h2.next = ListNode(3)h2.next.next = ListNode(4)

h = Solution().mergeTwoLists(h1, h2)while h is not None:    print(h.val, end="->")    h = h.next

原文地址:https://www.cnblogs.com/Smart-Cat/p/12249540.html

时间: 2024-10-01 00:37:42

leedCode练题——21. Merge Two Sorted Lists(照搬大神做法)的相关文章

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

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

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

LeetCode记录之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 fir

【leetcode】 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. 解题分析: 再基础不过的题了,直接看代码吧^-^ 具体代码: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode

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. 分析: 就是使用两个指针,让较小的归并到新的list上: 代码: 方法一: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2