LeetCode——Merge Two Sorted Lists

来源:互联网 发布:如何考上清华知乎 编辑:程序博客网 时间:2024/05/14 04:26

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 = Noneclass 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写这题的人很少,没法看到比较时间,尴尬。

原创粉丝点击