LeetCode singly linked list - delete nodes in the linked list

Address: https://leetcode.cn/problems/delete-node-in-a-linked-list
insert image description here
Solution idea: idea: node is the node to be deleted, change the value of the second node to the value of the third node , that is, modify the value of node 5 to 1 and then point the value of the third node to empty

public class 删除链表中的节点 {
    
    
    public class ListNode {
    
    
      int val;
      ListNode next;
      ListNode(int x) {
    
     val = x; }
  }
    public void deleteNode(ListNode node) {
    
    
        node.val = node.next.val;
        //node.next.next=null
        node.next = node.next.next;
    }
}

おすすめ

転載: blog.csdn.net/m0_62491934/article/details/128739236