• This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Thank you very much for reading this article. Welcome [👍 like] [⭐ collect] [📝 comment] ~ Give up is not difficult, But insists it must be very cool ~ hope we all can be a little bit of progress every day ~ in this paper, from two headed white hat original ~ https://juejin.cn/user/2771185768884824/posts blog


02.03. Delete intermediate nodes:

If a node in a linked list is neither the head node nor the tail node, it is called an “intermediate node” of the list.

Given an intermediate node of a linked list, implement an algorithm to remove that node from the list.

For example, an incoming node C (in a-> B -> C -> D -> E -> F) is removed and the remaining list is A -> B -> D -> E -> F

Sample 1

Input: node 5 (in one-way linked list 4->5->1->9) Output: Does not return any data, removes the incoming node 5 from the linked list, making the linked list 4->1->9Copy the code

Analysis of the

  • The previous node of the linked list has the address of the next node. Generally, deleting a node directly connects the previous node of the current node with the next node of the current node.
  • In this case, only the node to be deleted is passed in. Therefore, the address of the current node cannot be changed. From a business point of view, we don’t care about the address of the list, we care about the value inside, so we just make it look like the value is deleted, even if the current node is deleted.
  • Rust was officially denied the title, so the owner had to give it up.

Answer key

java

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; }} * * /
class Solution {
    public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; }}Copy the code

c

/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; *}; * /
void deleteNode(struct ListNode* node) {
    node->val = node->next->val;
    node->next = node->next->next;
}
Copy the code

c++

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; * /
class Solution {
public:
    void deleteNode(ListNode* node) { node->val = node->next->val; node->next = node->next->next; }};Copy the code

python

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
    def deleteNode(self, node) :
        """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """
        node.val = node.next.val
        node.next = node.next.next
Copy the code

go

/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */
func deleteNode(node *ListNode) {
    node.Val = node.Next.Val
	node.Next = node.Next.Next
}
Copy the code


Portal: https://leetcode-cn.com/problems/delete-middle-node-lcci/