237. 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 Is 1, 4
and You is given the third node with Value 3
, the linked list should become 1, 2, 4
after calling your function.
Main topic:
Given a node in a single-linked list, delete the node.
Ideas:
Because you cannot know the previous node of this node, you can exchange information from the node that is currently being deleted to the next node of the node. Then delete the next node. This allows the node to be deleted.
/** * 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) { if (Null == node) return ; Listnode * next = node->next; node-> val = next->val; node->next = next- >next; delete next; }};
The topic is not very understood.
2016-08-12 21:05:17
This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1837436
Leetcode 237. Delete Node in a Linked list linked list