[Data structure] The sea of questions, it's all water (2) Merge two ordered linked lists and copy the linked list with random pointers

The data structure of caiji college students - brush topic 2
I think careful readers have discovered that there are only two topics today, should caiji college students also compromise with the times and turn to fast food reading?
Obviously not, it's just that the college students have been drunk with boiled water recently, and they will be fine in a few days.

Okay, I can't write anymore, let's get down to business.


Merge two sorted linked lists

Merge two ascending linked lists into a new ascending linked list and return. The new linked list is formed by splicing all the nodes of the given two linked lists.

Example 1:

Input: l1 = [1,2,4], l2 = [1,3,4]
Output: [1,1,2,3,4,4]

Example 2:

Input: l1 = [], l2 = []
Output: []

Example 3:

Input: l1 = [], l2 = [0]
Output: [0]

This problem is not a problem, but it is relatively easy to solve.

ideas

Build a new linked list and insert the smaller head node of the linked list in turn.

Since it is a tail deletion, we have to consider the condition that the linked list is empty . In order to simplify the code, we use the head node with the sentinel bit in this question .

 struct ListNode* Head=(struct ListNode*)malloc(sizeof(struct ListNode));
 struct ListNode* Tail=Head;
 Head->next=NULL;

Some notes:

  • When one of the linked lists ends, we just need to take the other linked list directly.
  • The return value is the node after the sentinel bit.
  • Head->next=NULLIt is to prevent the situation of two empty linked lists in example 2.
  • Remember to release the Sentinel at the end.

code

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    
    
    struct ListNode* Head=(struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* Tail=Head;
    Head->next=NULL;

    while(list1&&list2)
    {
    
    
        if(list1->val<list2->val)
        {
    
    
            Tail->next=list1;
            Tail=list1;
            list1=list1->next;
        }
        else
        {
    
    
            Tail->next=list2;
            Tail=list2;
            list2=list2->next;
        }
    }

    if(list1)
    {
    
    
        Tail->next=list1;
    }
    if(list2)
    {
    
    
        Tail->next=list2;
    }

    struct ListNode* ret=Head->next;
    free(Head);
    return ret;
}

Copy linked list with random pointer

The topic is long, and the hearts of vegetable chicken college students are cold.

Then let's briefly summarize what this topic wants us to do: copy the linked list given by the topic.

I'm familiar with this, Caiji college students thought, I've been touching the keyboard for so many years, and what I'm good at is ctrl+c and ctrl+v.

So how can this question be ranked as medium difficulty? Let's take a look at the example:

the most troublesome thing in this question is to copy the random too.

However, according to the research of Caiji college students, there is a very subtle solution, and I will share it with you quickly.

Solution 1

  1. Insert a copy of the same node after each node.
  2. The cur and copy pointers traverse the linked list, and the random of copy is the last bit of random of cur. (If random points to null, it points directly to null)
  3. Disconnect the two linked lists.

code

struct Node* copyRandomList(struct Node* head) {
    
    
    struct Node* cur=head;
    while(cur)
    {
    
    
        struct Node* copy=(struct Node*)malloc(sizeof(struct Node));
        copy->next=cur->next;
        copy->val=cur->val;
        cur->next=copy;
        cur=cur->next->next;
    }

    cur=head;
    while(cur)
    {
    
    
        struct Node* copy=cur->next;
        if(cur->random==NULL)
        {
    
    
            copy->random=NULL;
        }
        else
        {
    
    
            copy->random=cur->random->next;
        }
        cur=cur->next->next;
    }
	struct Node* copytail=NULL;
	struct Node* copyhead=NULL;
    cur=head;
    while(cur)
    {
    
    
        struct Node* copy=cur->next;
        struct Node* next=cur->next->next;

        cur->next=next;

        if(copyhead==NULL)
        {
    
    
            copyhead=copytail=copy;
        }
        else
        {
    
    
            copytail->next=copy;
            copytail=copy;
        }
        cur=next;
    }
    return copyhead;
}

Solution 2

To be more violent, first copy the nodes in the linked list except random, find the absolute position of the node pointed to by random in the original linked list, and determine the point of random in the new linked list through the absolute position.

The time complexity of this method is O(N*N), which is obviously not as good as the previous solution, so it is just to provide an idea.
Not spelled out.


finally

Drawing pictures... a lot of pictures to draw... drawing... woo woo drawing pictures drawing pictures more drawing pictures... in short, only drawing pictures.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324127247&siteId=291194637