Sword refers to Offer 18. Delete the node of the linked list (C++) double pointer

Given the head pointer of the singly linked list and the value of a node to be deleted, define a function to delete the node.
Return the head node of the deleted linked list.
Note: This question has been changed compared to the original question

Example 1:

输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

Example 2:

输入: head = [4,5,1,9], val = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

Description:

The problem guarantees that the values ​​of the nodes in the linked list are different from each other.
If you use C or C++ language, you don’t need to free or delete the deleted node.

Problem-solving ideas:

In this question, deleting the node with the value of val requires two steps: locating the node and modifying the reference.

Locate the node: Traverse the linked list until head.val == val and jump out to locate the target node.
Modify the reference: Set the predecessor node of the node cur as pre and the successor node as cur.next; then execute pre.next = cur.next to delete the cur node.

Insert picture description here

Algorithm flow:

1. Special case processing : When the head node head should be deleted, just return head.next directly.
2. Initialization : pre = head, cur = head.next.
3. Locate the node : Jump out when cur is empty or the value of cur node is equal to val.
3-1. Save the current node index, that is, pre = cur.
3-2. Traverse the next node, that is, cur = cur.next.
4. Delete node : If cur points to a node, execute pre.next = cur.next; if cur points to nullnull, it means that the linked list does not contain a node with a value of val.
5. Return value : Return the head node of the linked list.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
    
    
public:
    ListNode* deleteNode(ListNode* head, int val) {
    
    
        if(head->val == val) return head->next;//当头节点为val时,返回头节点的下一个节点
        ListNode *pre = head, *cur = head->next;//pre为赋值头节点
        //cur赋值为头节点下一个节点
        //当前节点非空 且 当前节点的值不等于val
        
        while(cur != nullptr && cur->val != val) {
    
    
        //遍历链表
            pre = cur;
            cur = cur->next;
        }

		//此时因此了条件cur->val == val
        if(cur != nullptr) pre->next = cur->next;//删除当前节点
        return head;
    }
};

Complexity analysis:

Time complexity O(N): N is the length of the linked list. The delete operation needs to loop N/2 times on average, and the worst is N times.
Space complexity O(1): cur, pre occupies extra space of constant size.

Author: jyd
link: https: //leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/solution/mian-shi-ti-18-shan-chu-lian-biao -de-jie-dian-sh-2/
Source: LeetCode (LeetCode)
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/qq_30457077/article/details/114704075