LeetCode Entry

237. Delete Node in a Linked List

05.05.2024 medium 2024 kotlin

Delete current node in a Linked List

237. Delete Node in a Linked List medium blog post substack youtube 2024-05-05_08-14.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/593

Problem TLDR

Delete current node in a Linked List #medium

Intuition

The O(n) solution is trivial: swap current and next values until the last node reached. There is an O(1) solution exists, and it’s clever: remove just the next node.

Approach

No Rust solution, as there is no template for it in leetcode.com.

Complexity

  • Time complexity: \(O(1)\)

  • Space complexity: \(O(1)\)

Code


    fun deleteNode(node: ListNode?) {
        node?.`val` = node?.next?.`val`
        node?.next = node?.next?.next
    }


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