鱼C论坛

 找回密码
 立即注册
查看: 688|回复: 6

[技术交流] 【朱迪的LeetCode刷题笔记】】21. Merge Two Sorted Lists #Easy #Python

[复制链接]
发表于 2023-5-24 10:09:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Judie 于 2023-5-29 21:06 编辑

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.

Example 1:
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

Example 2:
Input: list1 = [], list2 = []
Output: []

Example 3:
Input: list1 = [], list2 = [0]
Output: [0]


Constraints:
The number of nodes in both lists is in the range [0, 50].
-100 <= Node.val <= 100
Both list1 and list2 are sorted in non-decreasing order.

  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. #     def __init__(self, val=0, next=None):
  4. #         self.val = val
  5. #         self.next = next
  6. class Solution(object):
  7.     def mergeTwoLists(self, list1, list2):
  8.         """
  9.         :type list1: Optional[ListNode]
  10.         :type list2: Optional[ListNode]
  11.         :rtype: Optional[ListNode]
  12.         """
  13.    
复制代码


https://leetcode.com/problems/me ... on-dummy-explained/

Solution 1
by using variables dummy and temp
dummy is pointing to the start i.e. dummy makes sure we don;t loose the head of the thread (result list)
temp makes the linkes from node to node i.e we are traversing using temp
  1. class Solution:
  2.     def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:   
  3.         dummy = temp = ListNode(0)
  4.         while l1 != None and l2 != None: #1

  5.             if l1.val < l2.val: #2
  6.                 temp.next = l1 #3
  7.                 l1 = l1.next #4
  8.             else:
  9.                 temp.next = l2
  10.                 l2 = l2.next
  11.             temp = temp.next
  12.         temp.next = l1 or l2  #5
  13.         return dummy.next #6
复制代码

Solution 2
by doing recursion
  1. class Solution:
  2.     def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:   
  3.         if not l1 or not l2:
  4.             return l1 or l2
  5.         
  6.         if l1.val <= l2.val: #1
  7.             l1.next = self.mergeTwoLists(l1.next, l2)
  8.             return l1
  9.         else: #2
  10.             l2.next = self.mergeTwoLists(l1, l2.next)
  11.             return l2
复制代码


Judy
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-27 13:33:08 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-27 13:33:57 | 显示全部楼层
我觉得我也得慢慢习惯看英文,有时候中文的机翻有点看不懂什么意思
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-27 22:46:07 | 显示全部楼层
yinda_peng 发表于 2023-5-27 00:33
我觉得我也得慢慢习惯看英文,有时候中文的机翻有点看不懂什么意思

嗯嗯!你生活在哪啊 我是生活在北美
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-27 22:46:58 | 显示全部楼层

别说了别说了 这题我没写出来 看的别人的solution

更离谱的是 我翻我记录 这题我一两年前拿c写出来过
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-28 13:09:16 | 显示全部楼层
Judie 发表于 2023-5-27 22:46
嗯嗯!你生活在哪啊 我是生活在北美

在湖南呐,小城市出来的,衡阳人
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-5-29 02:42:30 | 显示全部楼层
yinda_peng 发表于 2023-5-28 00:09
在湖南呐,小城市出来的,衡阳人

那你英文真不错!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 11:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表