Delete Node in the Middle of Singly Linked List

题目:

Given 1->2->3->4, and node 3. return 1->2->4

分析:

这道题目比较有意思,一般删除链表中的结点,需要access前一个结点的,挪动他的next指针,但是这题目只给你当前结点的access,所以只好强行修改这个指针的值(也就是其next的地址)...

解法:

class Solution {
public:
    /**
     * @param node: a node in the list should be deleted
     * @return: nothing
     */
    void deleteNode(ListNode *node) {
        // write your code here
        *node = *(node->next);
    }
};

results matching ""

    No results matching ""