LeetCode Merge Two Sorted Lists

class Solution {
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        ListNode* p = l1;
        ListNode* q = l2;
        ListNode* head = NULL;
        ListNode* tail = NULL;
        ListNode* node = NULL;
        
        while (p != NULL && q != NULL) {
            if (p->val <= q->val) {
                node = p;
                p = p->next;
            } else {
                node = q;
                q = q->next;
            }
            add_node(head, tail, node);        
        }
        while (p != NULL) {
            add_node(head, tail, p);
            p = p->next;
        }
        while (q != NULL) {
            add_node(head, tail, q);
            q = q->next;
        }
        return head;
    }

    void add_node(ListNode* &head, ListNode* &tail, ListNode* node) {
        if (head == NULL) {
            head = node;
            tail = node;
        } else {
            tail->next = node;
            tail = node;
        }
    }
};

水一发

第二轮:

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.

最后可以直接黏贴剩余链表,不用一个一个在转移了

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
14         if (l1 == null || l2 == null) {
15             return l1 == null ? l2 : l1;
16         }
17         ListNode head = null;
18         ListNode prev = null;
19         ListNode h1 = l1;
20         ListNode h2 = l2;
21         while (h1 != null && h2 != null) {
22             ListNode append = null;
23             if (h1.val <= h2.val) {
24                 append = h1;
25                 h1 = h1.next;
26             } else {
27                 append = h2;
28                 h2 = h2.next;
29             }
30             append.next = null;
31             if (head == null) {
32                 head = append;
33                 prev = append;
34             } else {
35                 prev.next = append;
36                 prev = append;
37             }
38         }
39         ListNode last = null;
40         if (h1 != null) {
41             last = h1;
42         }
43         if (h2 != null) {
44             last = h2;
45         }
46         if (prev != null) {
47             prev.next = last;
48         }
49         return head;
50     }
51 }

 看了一下参考,加入一个空链表头的话可以省去许多对空值的判断处理:

/**
 * 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 holder(0);
        ListNode* prev = &holder;
        while (l1 != NULL && l2 != NULL) {
            if (l1->val <= l2->val) {
                prev->next = l1;
                l1 = l1->next;
            } else {
                prev->next = l2;
                l2 = l2->next;
            }
            prev = prev->next;
        }
        if (l1 != NULL) {
            prev->next = l1;
        }
        if (l2 != NULL) {
            prev->next = l2;
        }
        return holder.next;
    }
};

 

posted @ 2014-05-29 13:52  卖程序的小歪  阅读(175)  评论(0编辑  收藏  举报