close

題目

  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

想法

  linked list 要刪除節點,其實就是跳過這個連結,所以只要用下個節點來取代要被刪除的節點,即可完成。

code

void deleteNode(struct ListNode* node) {
    node->val = node->next->val;
    node->next = node->next->next;
}

arrow
arrow
    文章標籤
    leetcode C
    全站熱搜
    創作者介紹
    創作者 Davis 的頭像
    Davis

    Epoch

    Davis 發表在 痞客邦 留言(0) 人氣()