21:合并两个有序链表

问题描述

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

图解

在这里插入图片描述

思路

经典的数据结构问题。如果想省一个结点,就先判断出l1和l2哪个小,让head指向小的。(方法一)
如果想程序写的简洁一点,那么就弄出来一个头结点,这样的话对以后结点的处理就归一化了。(方法二)
如果觉得程序还不够简洁,那么大可以用递归的方式来实现。只不过递归调用栈的深度成了个大问题。(方法三)

提醒:做链表问题,先画图,再写代码。

方法一

Java版

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null){
            return l2;
        }
        if(l2 == null){
            return l1;
        }
        ListNode head;
        if(l1.val > l2.val){
            head = l2;
            l2 = l2.next;
        }else{
            head = l1;
            l1 = l1.next;
        }
        ListNode cur = head;
        while(l1!=null&&l2!=null){
            if(l1.val > l2.val){
                cur.next = l2;
                l2 = l2.next;
            }else{
                cur.next = l1;
                l1 = l1.next;
            }
            cur = cur.next;
        }
        if(l1!=null){
            cur.next = l1;
        }else{
            cur.next = l2;
        }
        return head;
    }

Python版

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

class Solution:
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        flag = True
        head = None
        current = None
        while l1 or l2:
            if flag:
                flag = False
                if not l1:
                    return l2
                if not l2:
                    return l1
                if l1.val < l2.val:
                    head = l1
                    current = head
                    l1 = l1.next
                else:
                    head = l2
                    current = head
                    l2 = l2.next
            else:
                if not l1:
                    current.next = l2
                    return head
                if not l2:
                    current.next = l1
                    return head
                if l1.val < l2.val:
                    current.next = l1
                    current = l1
                    l1 = l1.next
                else:
                    current.next = l2
                    current = l2
                    l2 = l2.next
        return head

方法二

Java版

public ListNode mergeTwoLists1(ListNode l1, ListNode l2){
        ListNode head = new ListNode(217);
        ListNode cur = head;
        while(l1!=null&&l2!=null){
            if(l1.val<l2.val){
                cur.next = l1;
                l1 = l1.next;
            }else{
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        cur.next = l1 == null?l2:l1;
        return head.next;
    }

方法三

Java版

public ListNode mergeTwoLists2(ListNode l1, ListNode l2){
        if(l1 == null){
            return l2;
        }
        if(l2 == null){
            return l1;
        }
        if(l1.val < l2.val){
            l1.next=mergeTwoLists(l1.next,l2);
            return l1;
        }else{
            l2.next=mergeTwoLists(l1,l2.next);
            return l2;
        }
    }

Python版

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

class Solution:
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if not l1 or l2:
        	# 点睛之笔,如果l1为真,则返回l1,否则返回l2
            return l1 or l2 # 还新get了这种写法
        if l1.val < l2.val:
            l1.next = self.mergeTwoLists(l1.next,l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1,l2.next)
            return l2
发布了333 篇原创文章 · 获赞 22 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_41687289/article/details/104569434