Linked list for brushing questions (1)


foreword

The following are the interview questions that linked lists are often tested. I will summarize and explain them here, and I will explain them one by one in a step-by-step manner.
Of course, you need to have the basis of linked lists.


1. Remove linked list elements

1. Topic introduction

The topic is 203. Remove linked list elements
insert image description here

2. Ideas

There are two approaches to this question

1. Use a head node with a sentinel bit
2. Write separately when the head node is empty and when the head is deleted

3. Code

1. Head node with sentinel bit

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* removeElements(struct ListNode* head, int val){
    
    
    struct ListNode*dummyHead=(struct ListNode*)malloc(sizeof(struct ListNode));
    dummyHead->next=head;
    struct ListNode*prev= dummyHead;
    struct ListNode*cur=head;
    while(cur)
    {
    
    
        struct ListNode*temp=cur;
        if(cur->val==val)
        {
    
    
            struct ListNode*next=cur->next;
            prev->next=next;
        }
        else
        {
    
    
            prev=cur;  
        }
        cur=cur->next;   
    }
    return dummyHead->next;
}

2. No sentinel head node

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* removeElements(struct ListNode* head, int val){
    
    
    struct ListNode* prev=head;
    if(prev==NULL)
    {
    
    
        return head;
    }
    struct ListNode*cur=prev->next;
    while(cur)
    {
    
    
        if(prev->val==val&&prev->next!=NULL)//需要头删的情况,且之后有元素时
        {
    
    
            prev=prev->next;
            head=prev;
            cur=prev->next;
            
        }
        else if(cur->val==val)
        {
    
    
            prev->next=cur->next;
            cur=cur->next;
        }
        else if(cur->val!=val)
        {
    
    
            prev=cur;
            cur=cur->next;
        }
    }
    if(prev->val==val&&prev->next==NULL)//只有头节点的,而且需要头删的
    {
    
    
        head=NULL;
        return head;
    }
    return head;

}

2. Reverse linked list

1. Topic introduction

The topic is in 206. Reverse linked list
insert image description here

2. Ideas

This question uses three pointers, prev saves the address of the previous node, cur saves the current address, and cur also needs to change the next node, next saves the address of the next node of cur and finally returns to
insert image description here
prev

3. Code

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* reverseList(struct ListNode* head){
    
    
    struct ListNode*prev,*cur,*next;
    prev=NULL;
    cur=head;
    while(cur)
    {
    
    
        next=cur->next;
        cur->next=prev;
        prev=cur;
        cur=next;
    }
    return prev;
}

3. The middle node of the linked list

1. Topic introduction

The topic is 876. The middle node of the linked list in Lituo

insert image description here

2. Ideas

If you have done the double-pointer question of the array, then you will solve this question quickly. If you can’t understand my method quickly, you can go to the previous array question to read it. The double-pointer method will help you It will be perfect, well, even if you don't understand the double pointer method, you can quickly understand my method

We can use a slow pointer and a fast pointer. The speed of the fast pointer is twice that of the slow pointer, that is, every time the slow pointer takes one step, the fast pointer takes two steps. We assume that the length of this linked list is n, and the fast pointer completes this When the list is linked, the slow pointer just points to the middle of the linked list

3. Code

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* middleNode(struct ListNode* head){
    
    
        struct ListNode*slow=head;
        struct ListNode*fast=head;
        while(fast&&fast->next)
        {
    
    
            slow=slow->next;
            fast=fast->next->next;   
        }
        return slow;
     
}

4. The k-th node from the bottom of the linked list

1. Topic introduction

The title is at the k-th last node in the Niuke linked list
insert image description here

2. Ideas

If you have done one of my previous questions, then this question is very easy to understand

We can make two pointers, one slow and one fast, let fast take k steps first, and then when fast finishes, slow is the kth node from the bottom

3. Code

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

/**
 * 
 * @param pListHead ListNode类 
 * @param k int整型 
 * @return ListNode类
 */
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
    
    
    // write code here
    struct ListNode* fast=pListHead;
    struct ListNode* slow=pListHead;
    while(k--)
    {
    
    
        if(fast==NULL)
        {
    
    
            return NULL;
        }
        fast=fast->next;
    }
    while(fast)
    {
    
    
        fast=fast->next;
        slow=slow->next;
    }
    return slow;
}

Guess you like

Origin blog.csdn.net/Ruiren_/article/details/129464689