[Leetcode Data Structure Algorithm Question] 203. Remove Linked List Elements (Linked List)

Topic content:

Given the head node head of a linked list and an integer val, please delete all nodes in the linked list that satisfy Node.val == val, and return the new head node.

insert image description here
leetcode topic link (click to jump)


Thought analysis

This is a relatively simple singly linked list problem, which belongs to a unidirectional unleaded acyclic linked list. Note that the head node here is not the kind of "head node" that takes the lead. The head node here is equivalent to the name of the linked list (giant pit).
Although the difficulty of the problem itself is not large, if you don't analyze it carefully, and immediately program the problem as soon as you see it, it may take a lot of time to debug.
In fact, for linked list-related topics, no matter how difficult it is, we should draw a picture to analyze it first, because such topics usually involve a variety of conditions, and these conditions are often what we need to pay special attention to when programming.
Take this question as an example:
Case 1: A normal scenario with multiple valid nodes

Suppose a pointer cur is created, and cur first points to head. Then cur goes back to find the node with value = val. When cur finds the corresponding node, we want to delete the node at this time, but we find that this is a singly linked list, and we have a node in front of cur at this time. Don't know. If we want to traverse the linked list from the beginning to find a node in front of cur at this time, it will be very "trouble".
At this time, we should think that at the beginning, we may need to create two pointers. One is used to traverse the linked list to find the node whose value is val, and the other is to find the previous node of the node when the corresponding node is found, and complete the operation of deleting the node.
insert image description here

Case 2: Special scenario with only one valid node
insert image description here

It can be seen that the method used in case 1 cannot successfully solve the case where there is only one valid node (and the value of this node happens to be val). Therefore, in the actual programming process, case 1 and case 2 should be discussed separately. (Of course, the reason for discussing case 1 and case 2 separately here is because there are many linked list topics in which the two cases are handled in different ways. Cultivating such thinking can effectively help us analyze and reduce mistakes.)

Case 3: Extreme scenario of empty linked list
insert image description here

When there is only the head node in the linked list, we should return directly without any processing. It can be seen that the priority of judgment in this case is the highest, and we need to judge at the beginning.


Function interface implementation

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


struct ListNode* removeElements(struct ListNode* head, int val){
    
    
    //判断是否为空链表
    if(head == NULL)
        return head;
    struct ListNode* prev = NULL;
    struct ListNode* cur = head;
    //cur遍历链表
    while(cur){
    
    
        //cur指向的结点值不为val时,cur往后走
        while(cur && (cur->val != val)){
    
    
            prev = cur;//将当前结点的位置给prev
            cur = cur->next;//cur走向下一个结点
        }
        if(cur == head){
    
    //删除的结点是头节点指向的结点
            head = head->next;//头节点指向的位置向后移动
            struct ListNode* next = cur->next;
            free(cur);
            cur = next;
        }
        else if(cur){
    
    //删除的结点不是头节点指向的结点且cur不为空
            struct ListNode* next = cur->next;
            free(cur);
            cur = next;
            prev->next = cur;
        }
    }
    return head;
}

insert image description here

Guess you like

Origin blog.csdn.net/QIYICat/article/details/122373645