LeetCode 237: Delete Node in a Linked List Solution

Master LeetCode problem 237 (Delete Node in a Linked List), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

237. Delete Node in a Linked List

Problem Explanation

Explanation:

To delete a node from a singly-linked list without knowing the head node, we can follow these steps:

  1. Copy the value from the next node to the current node.
  2. Update the current node's next pointer to skip the next node.

This way, we effectively "delete" the given node by overwriting its value and changing the next pointer to skip the next node.

Time complexity: O(1)
Space complexity: O(1)

:

Solution Code

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

public void deleteNode(ListNode node) {
    node.val = node.next.val;
    node.next = node.next.next;
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 237 (Delete Node in a Linked List)?

This page provides optimized solutions for LeetCode problem 237 (Delete Node in a Linked List) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 237 (Delete Node in a Linked List)?

The time complexity for LeetCode 237 (Delete Node in a Linked List) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 237 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 237 in Java, C++, or Python.

Back to LeetCode Solutions