[剑指Offer] 25_合并两个排序链表

版权声明:Tian Run https://blog.csdn.net/u013908099/article/details/86175811

题目

输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然使递增排序的。

例:

L1:1->3->5->7
L2:2->4->6->8
L:1->2->3->4->5->6->7->8


思路

  1. 两个指针分别遍历L1、L2,将较小的连入。思路很简单,但是代码写出来却各有差异。
    1. 时间复杂度:O(n)
    2. 空间复杂度:O(1)

代码

思路1:

def merge_sorted_lists( l1, l2):
    """
    :param l1: ListNode 1 head
    :param l2: ListNode 2 head
    :return: merged L head
    """
    if not l1 or not l2:
        return l1 or l2
    if l1.val <= l2.val:
        main = l1
        sub = l2
    else:
        main = l2
        sub = l1
    last = main
    while sub and last.next:
        if last.next.val <= sub.val:
            pass
        else:
            temp = last.next
            last.next = sub
            sub = temp
        last = last.next
    last.next = sub
    return main

def merge_sorted_lists_2(l1, l2):
    if not l1 or not l2:
        return l1 or l2
    sham_head = ListNode(0)
    sham_head.next = l1
    node = sham_head
    while node.next:
        if node.next.val > l2.val:
            node.next, l2 = l2, node.next
        node = node.next
    node.next = l2
    return sham_head.next 

def merge_sorted_lists_3(l1, l2):
    sham_head = ListNode(0)
    node = sham_head
    while l1 and l2:
        if l1.val < l2.val:
            node.next = l1
            l1 = l1.next
        else:
            node.next = l2
            l2 = l2.next
        node = node.next
    node.next = l1 or l2
    return sham_head.next

思考

第1个函数是我以前做LeetCode时候写的,有很多不必要的语句。
第2个函数用了假头略去了第一次单独判断大小,代码简洁了点,但是仍然需要单独处理空输入。
第3个函数,逻辑清晰,代码简洁。
附LeetCode:

LeetCode 21. 合并两个有序链表

猜你喜欢

转载自blog.csdn.net/u013908099/article/details/86175811
今日推荐