Title Link: https://leetcode.com/problems/delete-node-in-a-linked-list/
Title:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
supposed the linked list Is 3
, the linked list should become 1, 2-4
after calling your function.
Problem-solving ideas: Delete the node specified in the single-linked list, there are generally two methods: the first is to traverse the previous node of the specified node to execute the next node of the specified node, and then delete the specified node, the second method is to assign the data value of the next node of the specified node to the specified node. Next, point the specified node to the next node in the node. The second method can obviously be used to solve the problem.
Example code:
public class Solution{class Listnode{int Val; ListNode Next; ListNode (int x) {val = x;}} public void Deletenode (ListNode node) {if (node==null) return; node.val=node.next.val;node.next=node.next.next;}}
"Leetcode OJ 237" Delete Node in a Linked List