In this Leetcode Delete Node in a Linked List problem solution 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.

Leetcode Delete Node in a Linked List problem solution


Problem solution in Python.

def deleteNode(self, node: ListNode, n: int) -> None:
    while node.val != n:
        node = node.next
    node.val = node.next.val
    node.next = node.next.next
    return



Problem solution in Java.

class Solution {
    public void deleteNode(ListNode node) {
        while(node.next.next!=null)
        {
            node.val=node.next.val;
            node=node.next;
        }
        node.val=node.next.val;
        node.next=null;
    }


Problem solution in C++.

class Solution {
public:
void deleteNode(ListNode* node) {
*node = *(node->next);
}
};


Problem solution in C.

void deleteNode(struct ListNode* node) {

struct ListNode *temp=node;
struct ListNode *temp1=NULL;

while(temp->next != NULL)
{
    temp->val=temp->next->val;
    temp1=temp;
    temp=temp->next;
}
temp1->next=NULL;
free(temp);
}