237. Delete Node in a Linked List

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.

原題鏈接:https://leetcode.com/problems/delete-node-in-a-linked-list/

思路
太簡單了吧,接口直接把node都給出來了,省掉了查找的時間。
更多鏈表操作可以看這篇文章《鏈表操作》

代碼(C)

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

    node->val = node->next->val;
    node->next = node->next->next;

}
發佈了140 篇原創文章 · 獲贊 97 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章