Delete Node in a Linked List LeetCode Solution

Problem – Delete Node in a Linked List

Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly.

It is guaranteed that the node to be deleted is not a tail node in the list.

Example 1:

Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.

Example 2:

Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.

Constraints:

  • The number of the nodes in the given list is in the range [2, 1000].
  • -1000 <= Node.val <= 1000
  • The value of each node in the list is unique.
  • The node to be deleted is in the list and is not a tail node

Delete Node in a Linked List LeetCode Solution in C++

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

Delete Node in a Linked List LeetCode Solution in Python

def deleteNode(self, node):
    node.val = node.next.val
    node.next = node.next.next

Delete Node in a Linked List LeetCode Solution in Java

public void deleteNode(ListNode node) {
    node.val=node.next.val;
    node.next=node.next.next;
}
Delete Node in a Linked List LeetCode Solution Review:

In our experience, we suggest you solve this Delete Node in a Linked List LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Delete Node in a Linked List LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Delete Node in a Linked List LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.