leetcode-237-delete node in a linked list

Problem Description:

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.

题目大意:

删除一个链表中的结点,你只能获取该结点的指针。

Solutions:

有点像脑筋急转弯。很简单,将这个结点的next结点拷贝到这个结点就可以了。

Code in C++:

/**
 * 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) {
            node->val=node->next->val;
            node->next=node->next->next;
        }
    };