237. Delete Node in a Linked List

来源:互联网 发布:淘宝哪个母婴店比较好 编辑:程序博客网 时间:2024/05/01 18:48
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 value3, the linked list should become 1 -> 2 -> 4 after calling your function.

java edition:

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public void deleteNode(ListNode node) {        if (node.next==null) return;        node.val=node.next.val;        node.next=node.next.next;                    }}

 

c edition:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */void deleteNode(struct ListNode* node){    if(node==0||node->next==0)return;    node->val=node->next->val;    node->next=node->next->next;        }


0 0