LeetCode——Merge Two Sorted Lists

本文介绍如何解决LeetCode上的第21题:合并两个有序链表。通过比较两个链表的节点值,创建一个新的有序链表。提供了C++、Java和Python三种语言的实现方式。
LeetCode——Merge Two Sorted Lists

#21

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

这个问题是要将两个有序链表合并起来,这题就是对链表操作的考查。弄好结构关系,先做头指针,以免弄乱。主要思路就是将两个链表的val进行比较,将较小值的节点加入新链表,直到一个链表为空,就可以直接将另一个链表直接插入新链表。
这一题是挺简单的一题,我的解题方法如下,没啥创意,就笔直的写下去了。

  • C++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */ 
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *h1 = new ListNode(0);
        ListNode *h2 = new ListNode(0);
        h1 -> next = l1;
        h2 -> next = l2;
        ListNode *h3 = new ListNode(0);
        ListNode *t = h3;
        if(h1 -> next == NULL)
            return l2;
        if(h2 -> next == NULL)
            return l1;
        while(h1 -> next != NULL || h2 -> next != NULL)
        {
            if(h1 -> next == NULL || (h2 -> next != NULL && h1 -> next -> val > h2 -> next -> val))
            {
                t -> next = h2 -> next;
                t = t ->next;
                h2 -> next = t -> next;
            }
            else
            {
                t -> next = h1 -> next;
                t = t -> next;
                h1 -> next = t -> next;
            }
        }
        return h3 -> next;
    }
};

这里我为了思路清晰,就写的比较详细,其实可以简化。少了几个头指针的操作,run time可以减少一点。但其实我觉得上面一种写法对理清链表的结构更有帮助。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *l3 = new ListNode(0);
        ListNode *h3 = l3;
        if(l1 == NULL)
            return l2;
        if(l2 == NULL)
            return l1;
        while(l1 != NULL || l2 != NULL)
        {
            if(l1 == NULL || (l2 != NULL && l1 -> val > l2 -> val))
            {
                h3 -> next = l2;
                l2 = l2 ->next;
                h3 = h3 -> next;
            }
            else
            {
                h3 -> next = l1;
                l1 = l1 -> next;
                h3 = h3 -> next;
            }
        }
        return l3 -> next;
    }
};
  • Java
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode l3 = new ListNode(0);
        ListNode h3 = dummy;

        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                h3.next = l1;
                l1 = l1.next;
            } else {
                lastNode.next = l2;
                l2 = l2.next;
            }
            h3 = h3.next;
        }

        if (l1 != null) {
            h3.next = l1;
        } else {
            h3.next = l2;
        }

        return l3.next;
    }
}

Java的解法,run time在所有答案中还行,所以我没去修改了。可以在二刷的时候,做最优解。一刷我觉得主要是理清自己的思路,如果觉得这题的思路自己没什么大问题,运行不是特别慢,就不用太钻牛角尖,多刷一题也好。

  • 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 l1 == None:
            return l2
        if l2 == None:
            return l1
        l3 = ListNode(0)
        h3 = l3
        while l1 and l2:
            if l1.val <= l2.val:
                h3.next = l1
                l1 = l1.next
                h3 = h3.next
            else:
                h3.next = l2
                l2 = l2.next
                h3 = h3.next
        if l2 == None:
            h3.next = l1
        else:
            h3.next = l2
        return l3.next

LeetCode上用python写这题的人很少,没法看到比较时间,尴尬。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值