https://leetcode-cn.com/problems/merge-two-sorted-lists/
# -*- 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 l1 is None and l2 is None: return if l1 is None or l2 is None: return l1 if l2 is None else l2 if l1.val > l2.val: return self.mergeTwoLists(l2, l1) res = ListNode(l1.val) node = res l1 = l1.next while l1 is not None and l2 is not None: if l1.val < l2.val: node.next = l1 l1 = l1.next else: node.next = l2 l2 = l2.next node = node.next while l1 is not None: node.next = l1 l1 = l1.next node = node.next while l2 is not None: node.next = l2 l2 = l2.next node = node.next return res if __name__ == '__main__': l1 = ListNode(-9) l2 = ListNode(3) # l3 = ListNode(4) l1.next = l2 # l2.next = l3 ll1 = ListNode(5) ll2 = ListNode(7) # ll3 = ListNode(4) ll1.next = ll2 # ll2.next = ll3 print(Solution().mergeTwoLists(l1, ll1))
思路:思路比较简单,依次遍历两个链表,比较当前标记位的大小,较小的重新链入新链表中。当有一个链表拆成空时说明另一个链表中任何一位都大。因此直接链入新链表的链尾即可。
思考:好像有的地方有点麻烦了傲,最后对单一链表插入链尾的操作不需要循环,只需要node.next = l1即可。毕竟已经是连好了的嘛。
0 条评论