Advertisement
237. Delete Node in a Linked List
EasyView on LeetCode
Time: O(1)
Space: O(1)
Approach
Copy the next node’s value into the current node and skip the next node (no access to the head is needed).
237.cs
C#
// Approach: Copy the next node’s value into the current node and skip
// the next node (no access to the head is needed).
// Time: O(1) Space: O(1)
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}
public class Solution
{
public void DeleteNode(ListNode node)
{
if (node.next != null)
{
node.val = node.next.val;
node.next = node.next.next;
}
}
}Advertisement
Was this solution helpful?