Leetcode---LeetCode21. Merge two ordered linked lists (linked list classic question)


foreword

Anxiety will not eliminate tomorrow's sorrow , it will only make you lose your strength today


提示:以下是本篇文章正文内容,下面案例可供参考

21. Merge two sorted linked lists

Merges two ascending lists into a new ascending list and returns. The new linked list is formed by splicing all the nodes of the given two linked lists.
insert image description here

Link:

21. Merge two ordered linked listslink

Method 1: Take the small tail plug

1.1 Code:

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    
    
    struct ListNode* head=NULL;
    struct ListNode* tail=NULL;
    if(list1==NULL)//判断是否是空链表
        return list2;
    if(list2==NULL)//判断是否是空链表
        return list1;
    while(list1&&list2)
    {
    
    
        if(list1->val<list2->val)
        {
    
    
            if(tail==NULL)
            {
    
    
                head=tail=list1;
            }
            else
            {
    
    
                tail->next=list1;
                tail=tail->next;
            }
            list1=list1->next;
        }
        else
        {
    
    
            if(tail==NULL)
            {
    
    
                head=tail=list2;
            }
            else
            {
    
    
                tail->next=list2;
                tail=tail->next;
            }
            list2=list2->next;
        }
    }
    if(list1)
    {
    
    
        tail->next=list1;
    }
    if(list2)
    {
    
    
        tail->next=list2;
    }
    return head;
}

1.2 Flowchart:

insert image description here

1.3 Note:

  • Pay attention to judging whether list1 and ;ist2 are empty linked lists
  • To consider the case of the first insertion is no node

Method 2: with sentinel position

The so-called linked list with a sentinel bit is an additional linked list node, which is the first node,its value field does not store anything, are referenced only for operational convenience.
If a linked list has sentinel nodes, then the first element of the linear list should be the second node of the linked list.

2.1 Code:

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    
    
    struct ListNode* head=NULL;
    struct ListNode* tail=NULL;
    head=tail=(struct ListNode*)malloc(sizeof(struct ListNode));
    if(list1==NULL)
        return list2;
    if(list2==NULL)
        return list1;
    while(list1&&list2)
    {
    
    
        if(list1->val<list2->val)
        {
    
    
     
            tail->next=list1;
            tail=tail->next;
            list1=list1->next;
        }
        else
        {
    
    
            tail->next=list2;
            tail=tail->next;
            list2=list2->next;
        }
    }
    if(list1)
    {
    
    
        tail->next=list1;
    }
    if(list2)
    {
    
    
        tail->next=list2;
    }
    struct ListNode* del=head;
    head=head->next;
    free(del);
    del=NULL;
    return head;
}

2.2 Flowchart:

insert image description here


Summarize

Ending, this is the end of today's Likou daily question content. If you want to know more in the future, please follow me. There are many methods that have not been written. I hope you guys can add them~

Guess you like

Origin blog.csdn.net/ljq_up/article/details/130350799