leetcode_7. Merging two sorted linked list

  • Merge two ordered list : the two ordered list into a new list and orderly return. The new list is by all nodes in a given mosaic composed of two lists.
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

A recursive method:

  • First, whether the handling of lists l1 and l2 is empty

  • Since l1 and l2 are sorted linked list, comparing the first number thereof, if l1 <l2, the recursive call incoming subsequent merge function l1-> next, and returns this time point most l1 summary of a comparison; if l1 > = 2, then the recursive call incoming subsequent merge function l2-> next, and returns this time point most l2 summary of a comparison;

  • Recursive end point:
    IF (L1 = = NULL) return L2;
    IF (L2 = = NULL) return L1;

  • This problem can be found to do directly:

 ListNode* p;    
 //等同于struct ListNode* p;
 p=(ListNode*)malloc(sizeof(ListNode)); 
 //等同于 p=(struct ListNode*)malloc(sizeof(struct ListNode)); 
  • In addition, I think the way to solve the problem using recursion hard, but hard to abstract First step, the second is really difficult to understand, I may now not yet reached capacity.
  • Write this question let me know what've learned is not necessarily completely understood, and a knowledge point there are many small details , such as I moved it to the full implementation devc, I need to create and add the two lists l1 and examples of l2, I think when I learned of the list is still quite good, suddenly do not know how to create a written list and link up. Recently wrote a blog is very fast, now, the memory is still not strong enough and deep, I often need to use the knowledge learned and pay attention.
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}  //类似类的构造函数,参数val和next的默认参数为传入的x和NULL
 * };
 */
class Solution {
    public:
     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) 
     {
        if(l1==NULL) return l2;
        if(l2==NULL) return l1;
        if(l1->val < l2->val)
        {
            l1->next = mergeTwoLists(l1->next,l2);
            return l1;
        }
        else
        {
            l2->next = mergeTwoLists(l1,l2->next);
            return l2;
        }
     }
};

Second iteration method:

  • The idea is actually very simple, two pointers p1 and p2 are traversing the two ordered list of tools. If p1-> val <p2-> val, continued in order to find an ordered list l1, until the two lists will be greater than the node by pointers linked, correspondence information changes, then the comparison continues until p2 is empty. Here direct draw a diagram to explain the process in which:
  • How, is not very simple idea ... This is the greatest feeling I have recently learned algorithms and data structures, the idea is not difficult, but it is actually quite hard to achieve.
  • Pointer road also need to hone.
class Solution {
    public:
     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if (l2 == NULL) return l1;
        if (l1 == NULL) return l2;
        ListNode* p1 = l1;  //p1可以访问到l1
        ListNode* p2 = l2;  //p2可以访问到l2
        ListNode* start = l1->val < l2->val ? l1 : l2;  //start指向两个有序链表中第一个结点较小的一个
        do {
            if (p1->val < p2->val)  //向p1链表中顺序往后遍历
            {
                while (p1->next != NULL && p1->next->val < p2->val)
                { 
                    p1 = p1->next; //若还是小于,则继续遍历直到不小于
                }
                l2 = p2->next;
                p2->next = p1->next;
                p1->next = p2;    //将p2指向的l2与p1衔接上
            } 
            else                  //向p2链表中顺序往后遍历
            {
                while (p2->next != NULL && p2->next->val <= p1->val)
                {
                    p2 = p2->next;
                }
                l2 = p2->next;
                p2->next = p1;
            }
            p2 = l2;
        }while (p2 != NULL);
        return start;
     }
};
发布了77 篇原创文章 · 获赞 19 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_42932834/article/details/95348435