Delete Node in a Linked List
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 1 -> 2 -> 3 -> 4
is and you were given the third node with value 3
, the linked list should become 1 -> 2 -> 4
a Fter calling your function.
Problem Solving Ideas:
This problem means that, given a node in a linked list, delete it. The topic does not have a given list header. The general idea is to move the nodes one after the other forward. The last node needs to be handled in a special way.
/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * listnode (int x): Val (x), Next (NULL) {} *}; */class Soluti On {public: void Deletenode (listnode* node) { listnode* pre=node; The title said it could not be the end of the node while (node->next!=null) { node->val = node->next->val; Pre=node; node=node->next; } Delete (node); pre->next=null; }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Leetcode] Delete Node in a Linked List