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 a node in a single-linked list, and the previous delete node is different, it is not to the head node, given is to delete the node pointer, generally delete the node to know the previous node, but this problem we do not know, so we can use to delete the node's value of the next node to overwrite the value of this node, Then delete the next node.
Code:
/** * 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) { //*node=*node->next; if (node==null| | Node->next==null) return; listnode* P=node; listnode* s=p->next; p->val=s->val; p->next=s->next; }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Delete Node in a Linked List