Leetcode21-Merge Two Sorted Lists

21. Merge Two Sorted Lists

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.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

题解:

合并两个已排序的链表:
思路是创建一个头结点ListNode result(0),让new_head指向这个头结点;然后比较两个链表,将较小的节点插入到new_head后右移该节点的指针和new_head指针,直到两个链表有一个为空指针;连接剩余的非空的链表;最后返回result.next。

My Solution(C/C++完整实现):

#include <stdio.h>
#include <iostream>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode * Merge(ListNode* pHead1, ListNode* pHead2)
    {
        ListNode result(0);
        ListNode *new_head = &result;
        while (pHead1 && pHead2) {
            if (pHead1->val < pHead2->val) {
                new_head->next = pHead1;
                pHead1 = pHead1->next;
            }
            else {
                new_head->next = pHead2;
                pHead2 = pHead2->next;
            }
            new_head = new_head->next;
        }
        if (pHead1) {
            new_head->next = pHead1;
        }
        if (pHead2) {
            new_head->next = pHead2;
        }
        return result.next;
    }
};

int main() {
    ListNode a(1);
    ListNode b(2);
    ListNode c(4);
    ListNode d(5);
    ListNode e(7);
    ListNode A(3);
    ListNode B(5);
    ListNode C(6);
    ListNode D(8);
    ListNode E(10);
    a.next = &b;
    b.next = &c;
    c.next = &d;
    d.next = &e;
    A.next = &B;
    B.next = &C;
    C.next = &D;
    D.next = &E;
    Solution s;
    ListNode *result = s.Merge(&a, &A);
    while (result) {
        printf("%d->", result->val);
        result = result->next;
    }
    return 0;
}

结果:

1->2->3->4->5->5->6->7->8->10->

My Solution(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
        """
        new_head = ListNode(0)
        result = new_head
        while l1 and l2:
            if l1.val <= l2.val:
                new_head.next = l1
                l1 = l1.next
            else:
                new_head.next = l2
                l2 = l2.next
            new_head = new_head.next
        if l1:
            new_head.next = l1
        if l2:
            new_head.next = l2
        return result.next
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,160评论 0 10
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,351评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,471评论 0 23
  • 最近刚刚入职新公司.公司给配了台新的设备,等于说一切都是从头开始,昨天配环境用了一天时间.现在跟大家分享一下新人入...
    ZzQzz阅读 3,914评论 0 0
  • 雨一直下…… 灵魂被雨水浸泡了好久好久 时而有吝啬的太阳出现 晒一晒那滴水的灵魂 你说 一个人会为了另一个人去改变...
    九月wll阅读 1,131评论 0 0