OJ练习10——T21 Merge Two Sorted Lists

合并有序链表,链表结构已给出。

要求返回的链表由原链表的节点构成,不再重新创建节点。

【思路】

数据结构入门算法。分别为两个链表设“滑块”,比较当前滑块数值的大小,小的就将返回链表的末尾指针指向它。

注意:

1.要为返回链表设立总是指向其尾部节点的标志,方便归入新节点。

2.考虑原始链表为空的情况。

【my code】

写的比较笨,但总归是对的。值得鼓励。

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

ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
    ListNode *head=NULL;
    ListNode *p=NULL;
    ListNode *q=NULL;
    if(l1==NULL&&l2==NULL)
        return NULL;
    else if(l1==NULL)
        return l2;
    else if(l2==NULL)
        return l1;
    if(l1->val<l2->val){
        head=l1;
        p=l1->next;
        q=l2;
    }
    else{
        head=l2;
        p=l1;
        q=l2->next;
    }
    ListNode *r=head;
    while(p!=NULL&&q!=NULL)
    {
        if(p->val<q->val){
            r->next=p;
            p=p->next;
            r=r->next;
        }
        else{
            r->next=q;
            q=q->next;
            r=r->next;
        }

    }
    if(p!=NULL)
        r->next=p;
    if(q!=NULL)
        r->next=q;
    return head;
}

【other code】

ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        ListNode *helper=new ListNode(0);//注意这里!换成listNode *helper=NULL是不可行的
        ListNode *head=helper;
        while(l1 && l2)
        {
             if(l1->val<l2->val) helper->next=l1,l1=l1->next;
             else helper->next=l2,l2=l2->next;
             helper=helper->next;
        }
        if(l1) helper->next=l1;
        if(l2) helper->next=l2;
        return head->next;
    }

【对比评价】

1.由于传入参数不是引用和const,实际传入的是副本,修改不会影响原来的链表。所以无需设立指针p,q,直接用l1,l2即可。

2.排除空链情况的思路太繁琐。注意标注的地方,为helper分配了一个节点,初始化其val=0;其next默认初始化为NULL,因此l1,l2为null时,返回head->next是没问题的。

如果hepler=null,则返回错误。这里helper充当head的尾节点指针。

【后记】

this is the first time that I made it all by myself in 35 minutes.

though there is a long way to go, it‘s still an encouragement for me.

时间: 2024-07-31 03:43:34

OJ练习10——T21 Merge Two Sorted Lists的相关文章

[LeetCode]148.Merge Two Sorted Lists

[题目] Sort a linked list in O(n log n) time using constant space complexity. [分析] 单链表适合用归并排序,双向链表适合用快速排序.本题可以复用Merge Two Sorted Lists方法 [代码] /********************************* * 日期:2015-01-12 * 作者:SJF0115 * 题目: 148.Merge Two Sorted Lists * 来源:https://

【leetcode】Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

Merge Two Sorted Lists &amp; Remove Nth Node From End of List

1.合并两个排好序的list 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. 2.删除list倒数第n个元素 Remove Nth Node From End of List Given a linked list,

[Leetcode][JAVA] Merge Two Sorted Lists &amp; Sort List

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. 两个排好序的链表拼接,只要用两个指针遍历链表即可. 可以借助一个helper指针来进行开头的合并.如果有一个遍历完,则直接把另一个链表指针之后的部分全部接在后面: 1

Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

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. 依次拼接 复杂度 时间 O(N) 空间 O(1) 思路 该题就是简单的把两个链表的节点拼接起来,我们可以用一个Dummy头,将比较过后的节点接在这个Dummy头之后.最后

[leetcode]Merge k Sorted Lists @ Python

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路:归并k个已经排好序的链表.使用堆这一数据结构,首先将每条链表的头节点进入堆中,然后将最小的弹出,并将最小的节点这条链表的下一个节点入堆,依次类推,最终形成的链表就是归

[Leetcode] Merge k sorted lists 合并k个已排序的链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:这题最容易想到的是,(假设有k个链表)链表1.2合并,然后其结果12和3合并,以此类推,最后是123--k-1和k合并.至于两链表合并的过程见merge two sorted lists的分析.复杂度的分析见JustDoIT的博客.算法复杂度:假设每个链表的平均长度是n,则1.2合并,遍历2n个

71. Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 两个方法: 方法1. 利用 STL 中的 multiset (根据结点内的值)自动对指针排序.空间 O(N), 时间 O(NlogN). (亦可用于 k 个无序链表).(AC: 164ms) /** * Definition for singly-linked

Merge Two Sorted Lists leetcode java

题目: 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. 题解: 这道题是链表操作题,题解方法很直观. 首先,进行边界条件判断,如果任一一个表是空表,就返回另外一个表. 然后,对于新表选取第一个node,选择两个表表头最小的那个作为新表表头,指针后挪. 然后同时遍历