leetcode-Merge Two Sorted Lists-21

原创 2016年05月31日 22:37:38

又是一个经典的问题,合并两个有序的链表。用归并排序,两个指针分别扫描一遍。复杂度O(n)。链表、指针的使用要小心。顺序很重要。

/**
 * 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* head;
        ListNode* tail;
        ListNode* p=l1;
        ListNode* q=l2;
        if(p==NULL) return l2;
        if(q==NULL) return l1;
        
        if(p->val <= q->val){
            head=tail=p;
            p=p->next;
            tail->next=NULL;
        }
        else{
            head=tail=q;
            q=q->next;
            tail->next=NULL;
        }
        while(p&&q){
            if(p->val <= q->val){
                tail->next=p;
                tail=p;
                p=p->next;
                tail->next=NULL;
            }
            else{
                tail->next=q;
                tail=q;
                q=q->next;
                tail->next=NULL;
            }
        }
        if(p) tail->next=p;
        else tail->next=q;
        return head;
    }
};

一开始我是这种写法, 区别只是我先声明一个临时的指针来赋值,结果RE了,我猜想是不能这么用,至今不明白

/**
 * 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* head;
        ListNode* tail;
        ListNode* p=l1;
        ListNode* q=l2;
        if(p==NULL) return l2;
        if(q==NULL) return l1;
        
        if(p->val <= q->val){
            ListNode* tmp;   //用一个临时的指针来赋值
            tmp->val=p->val;<span style="white-space:pre">	</span>//猜想是不是指针不能这么用!!!!
            tmp->next=NULL;
            head=tail=tmp;
            p=p->next;
            delete tmp;
        }
        else{
            ListNode* tmp;
            tmp->val=q->val;  //同理
            tmp->next=NULL;
            head=tail=tmp;
            q=q->next;
            delete tmp;
        }
        while(q&&p){
            if(p->val <= q->val){
                ListNode* tmp;<span style="white-space:pre">	</span>//同理
                tmp->val=p->val;
                tmp->next=NULL;
                tail->next=tmp;
                tail=tmp;
                p=p->next;
                delete tmp;
            }
            else{
                ListNode* tmp;   //同理
                tmp->val=q->val;
                tmp->next=NULL;
                tail->next=tmp;
                tail=tmp;
                q=q->next;
                delete tmp;
            }
        }
        if(p) tail->next=p;
        else tail->next=q;
        return head;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

21. Merge Two Sorted Lists leetcode(lists)

Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list sho...

leetcode 21. Merge Two Sorted Lists

每个链表一个游标,把小的数插入结果就行了。 #include #include #include struct ListNode* add(struct ListNode* l1,int n...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

LeetCode(21)Merge Two Sorted Lists

题目如下: Merge two sorted linked lists and return it as a new list. The new list should be made by spli...

Leetcode 21 Merge Two Sorted Lists

Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list sho...

【leetcode】21. Merge Two Sorted Lists

一、题目描述 Merge two sorted linked lists and return it as a new list. The new list should be made by sp...

[LeetCode]21.Merge Two Sorted Lists

【题目】 Merge two sorted linked lists and return it as a new list. The new list should be made by splic...

leetcode 21. Merge Two Sorted Lists

1.题目 Merge two sorted linked lists and return it as a new list. The new...

leetcode: (21) Merge Two Sorted Lists

【Question】 Merge two sorted linked lists and return it as a new list. The new list should be made by...
  • zk031
  • zk031
  • 2015-08-24 13:18
  • 113

<LeetCode OJ> 21. Merge Two Sorted Lists

21. Merge Two Sorted Lists My Submissions Question Total Accepted: 96394 Total Submissions: 2851...

leetcode.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 t...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)