Linked list related interview questions Chapter 2

Posted by edtlov on Wed, 09 Feb 2022 22:19:31 +0100

Catalogue of series articles


preface

1, Circular linked list

Circular linked list

1. Title Description

  1. Given a linked list, judge whether there are links in the linked list.
  2. If there is a node in the linked list that can be reached again by continuously tracking the next pointer, there is a ring in the linked list. In order to represent the rings in a given linked list, we use the integer pos to represent the position where the tail of the linked list is connected to the linked list (the index starts from 0). If pos is - 1, there is no ring in the linked list.
  3. Note: pos is not passed as a parameter, but only to identify the actual situation of the linked list. Returns true if there are links in the linked list. Otherwise, false is returned.


2. Problem solving ideas


The code is as follows:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head)
{
    struct ListNode* fast=head,*slow=head;
    while(fast&&fast->next)
    {
        fast=fast->next->next;
        slow=slow->next;
        if(fast==slow)
        {
            return true;
        }
    }
    return false;
}

2, Ring linked list advanced

Ring linked list advanced

1. Title Description

  1. Given a linked list, return the first node from the linked list into the ring. If the linked list is acyclic, null is returned.
    In order to represent the rings in a given linked list, we use the integer pos to represent the position where the tail of the linked list is connected to the linked list (the index starts from 0). If pos is - 1, there is no ring in the linked list. Note that pos is only used to identify the ring and is not passed to the function as a parameter.
  2. Note: it is not allowed to modify the given linked list.


2. Problem solving ideas


The code is as follows:

struct ListNode *detectCycle(struct ListNode *head) 
{
    struct ListNode* fast=head,*slow=head;
    while(fast&&fast->next)
    {
        fast=fast->next->next;
        slow=slow->next;
        if(slow==fast)
        {
            struct ListNode* meet=fast;
            while(meet!=head)
            {
                meet=meet->next;
                head=head->next;
            }
            return meet;
        }
    }
    return NULL;
}

3, Complex linked list with random pointer

Complex linked list with random pointer

1. Title Description

  1. Give you a linked list with a length of N. each node contains an additional random pointer random, which can point to any node or empty node in the linked list. Construct a deep copy of this linked list. The deep copy should consist of exactly n new nodes, in which the value of each new node is set to the value of its corresponding original node. The next pointer and random pointer of the new node should also point to the new node in the replication linked list, and these pointers in the original linked list and replication linked list can represent the same linked list state. The pointer in the copy linked list should not point to the node in the original linked list.
  2. For example, if there are two nodes X and Y in the original linked list, where x.random -- > y. Then the corresponding two nodes X and Y in the copy linked list also have x.random -- > y.
  3. Returns the header node of the copy linked list.
  4. A linked list composed of n nodes is used to represent the linked list in input / output. Each node is represented by a [val, random_index]:
  5. Val: one represents node Integer of val. random_index: the node index pointed by the random pointer (range from 0 to n-1); null if it does not point to any node. Your code only accepts the head node of the original linked list as the incoming parameter.



2. Problem solving ideas


The code is as follows:

struct Node* copyRandomList(struct Node* head) 
{
    if(head==NULL)
    {
        return NULL;
    }
    //1. The copy node is hung behind the original node
	struct Node* cur=head;
    while(cur)
    {
        struct Node* next=cur->next;
        struct Node* copy=(struct Node*)malloc(sizeof(struct Node));
        copy->val=cur->val;
        cur->next=copy;
        copy->next=next;
        cur=next;
    }
    //2. Handle the random of the copy node
    cur=head;
    while(cur)
    {
        struct Node* copy=cur->next;
        if(cur->random==NULL)
        {
            copy->random=NULL;
        }
        else
        {
            copy->random=cur->random->next;
        }
        cur=copy->next;
    }
    //3. Take down the copied nodes and link them together to restore the original linked list
    cur=head;
    struct Node* copyHead,*copyTail;
    copyHead=copyTail=(struct Node*)malloc(sizeof(struct Node));
    while(cur)
    {
        struct Node* copy=cur->next;
        struct Node* next=copy->next;
        //Tail insertion
        copyTail->next=copy;
        copyTail=copyTail->next;

        cur->next=next;
        cur=next;
    }
    struct Node* guard=copyHead;
    copyHead=copyHead->next;
    free(guard);
    return copyHead;
}

4, Insert and sort the linked list

Insert and sort the linked list

1. Title Description

  1. Insert and sort the linked list.
    The animation of insert sort is shown above. Starting from the first element, the linked list can be considered to have been partially sorted (represented by black).
    At each iteration, an element (indicated in red) is removed from the input data and inserted in place into the ordered linked list.
  2. Insert sorting algorithm:
    Insertion sorting is iterative, moving only one element at a time until all elements can form an ordered output list.
    In each iteration, insert sort only removes an element to be sorted from the input data, finds its appropriate position in the sequence, and inserts it.
    Repeat until all input data is inserted.

2. Problem solving ideas


The code is as follows:

struct ListNode* insertionSortList(struct ListNode* head)
{
    if(head==NULL||head->next==NULL)
    {
        return head;
    }
    //Starting condition
    struct ListNode* sortHead=head;
    struct ListNode* cur=sortHead->next;
    sortHead->next=NULL;
    while(cur)  //Termination condition cur is null
    {
        struct ListNode* next=cur->next;
        struct ListNode* p=NULL;
        struct ListNode* c=sortHead;
        while(c)
        {
            if(c->val>cur->val)
            {
                break;
            }
            else
            {
                p=c;
                c=c->next;
            }
        }
        if(p==NULL)  //Not a single node
        {
            cur->next=c;
            sortHead=cur; //cur as the head node
        }
        else        //Middle insertion
        {
            p->next=cur;
            cur->next=c;
        }
        cur=next;
    }
    return sortHead;
}

5, Delete duplicate nodes in the linked list. Duplicate nodes are not retained

Delete duplicate nodes in the linked list. Duplicate nodes are not retained

1. Title Description

  1. In a sorted linked list, there are duplicate nodes. Please delete the duplicate nodes in the linked list. The duplicate nodes are not retained and the chain header pointer is returned. For example, the linked list 1 - > 2 - > 3 - > 3 - > 4 - > 4 - > 5 is 1 - > 2 - > 5 after processing

2. Problem solving ideas


The code is as follows:

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead) 
    {
        if(pHead==NULL||pHead->next==NULL)
        {
            return pHead;
        }
        struct ListNode* prev=NULL;
        struct ListNode* cur=pHead;
        struct ListNode* next=pHead->next;
        while(next)
        {
           if(cur->val==next->val)
           {   
              while(next && cur->val==next->val)
              {
                 next=next->next;
              }
              while(cur!=next)
              {
                  struct ListNode* del=cur;
                  cur=cur->next;
                  free(del);
              }
              if(prev==NULL)
              {
                  pHead=cur;
              }
              else
              {
                  prev->next=cur;
              }
              if(next)
              {
                  next=next->next;
              }
           }
           else
           {
            prev=cur;
            cur=next;
            next=next->next;
           }
        }
        return pHead;
    }
};

summary

The above is what we want to talk about today. This paper only briefly introduces the solutions of several classic problems in the linked list. We will continue to release the solutions of the problems behind the linked list. I hope you can give us more support. Also, if there are any problems above, please understand my brother's advice, but it doesn't matter. It's mainly because I can insist. I hope some students who study together can help me correct them. But if you can be gentle, please tell me that love and peace are the eternal theme and love you all.

Topics: C data structure linked list pointer