leetcode 21 合并两个有序链表

https://www.bilibili.com/video/BV1xa411A76q?p=4&vd_source=cdfcf738e0429ec2cffe4778dd8dd0e4

 

# 迭代
# https://blog.csdn.net/m0_61661179/article/details/127205244 class Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: preindex = prevalue = ListNode(-1) while list1 and list2: if list1.val <= list2.val: prevalue.next = list1 list1 = list1.next else: prevalue.next = list2 list2 = list2.next prevalue = prevalue.next # 合并后,剩余部分,直接指完 prevalue.next = list1 if list1 is not None else list2 return preindex.next

  

posted @ 2024-03-17 13:21  刘恩福  阅读(2)  评论(0编辑  收藏  举报