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 -> 2 -> 3 -> 4
and you are given the third node with value 3
, the linked list should become 1 -> 2 -> 4
after calling your function.
Subscribe to see which companies asked this question
一般单链表删除节点的思路食遍历到该节点的前一个节点然后操作,这里直接跨过该节点的下一个节点。
void deleteNode(struct ListNode* node) {
if(node->next == NULL){
return;
}else{
node->val = node->next->val;
node->next = node->next->next;
}
return;
}