Delete Node from a Linked List Leetcode


Given a pointer to the header of a Linked List and a position >=0, delete the node at the given position.

Question:
You’re given the pointer to the head node of a linked list and the position of a node to delete. Delete the node at the given position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The list may become empty after you delete the node.
Input Format
You have to complete the Node* Delete(Node* head, int position) method which takes two arguments - the head of the linked list and the position of the node to delete. You should NOT read any input from stdin/console. position will always be at least 0 and less than the number of the elements in the list.
Output Format
Delete the node at the given position and return the head of the updated linked list. Do NOT print anything to stdout/console.
Sample Input
1 --> 2 --> 3 --> NULL, position = 0
1 --> NULL , position = 0
Sample Output
2 --> 3 --> NULL
NULL
Explanation
1. 0th position is removed, 1 is deleted from the list.
2. Again 0th position is deleted and we are left with empty list.

This problem is popular in HackerRank, GeeksForGeeks (here and here), LeetCode, and StackOverFlow A collection of hundreds of interview questions and solutions are available in our blog at Interview Question Solutions



SOLUTION APPROACH:

  1. Iterative:
    1. Create pointer to the head node, say curNode, keep track of the lastNode that was not matched. This is required if the node to be deleted is a last node of the list in which case, we need to update the next pointer of last node to null.
    2. Iterate through the nodes, keep track of an index
    3. If the given position matches the current index, delete the node by updating the value of current node as the value of next node
    4. If the position does not matches, update the index and update the current node pointer to the next node.
    5. return the headNode
  2. Recursive:
    1. recursively call the deleteNode(headNode, position) method by advancing the headNode to headNode.next and advancing the position to position-1.
Complexity: O(N), where N = no. of nodes in the list.

===============================
Java Source Code for Iterative approach:
===============================
/*
  Delete Node at a given position in a linked list 
  head pointer input could be NULL as well for empty list
  Node is defined as 
  class Node {
     int data;
     Node next;
  }
*/
    // This is a "method-only" submission. 
    // You only need to complete this method. 

void printList(Node head){
    //print the given input list
    Node a = head;
    //System.out.println("list is:");
    while(a!=null){
        System.out.print(a.data+"-->");
        a = a.next;
    }
}

Node Delete(Node head, int position) {

  // Complete this method
    if(head==null){
        return head; //no deletion on empty node
    }
    
    if (position==0){
        return head.next;
    }
    
    Node curNode = head;
    Node lastNode = head;
    int curIndex = 0;
    //printList(head);
    //System.out.println(">>>"+head.data+"..."+position);
    while(curNode!=null){
        //System.out.println(">>> curIndex vs position "+curIndex+"..."+position);
        if (position==curIndex){
            //delete this node
            //if the next node is not null
            if(curNode.next!=null){
                curNode.data = curNode.next.data;
                curNode.next = curNode.next.next;
            }else{
                //the next node is null
                lastNode.next = null;
            }
            break;
            //return lastNode;
        }
        lastNode = curNode;
        curIndex++;
        curNode = curNode.next;
    }
    return head;
}
==============================
Java Source code for recursive approach
==============================
Node Delete(Node head, int position) {
 if (head==null){
 return head;
}
if(position==0){
 return head.next;
}
head.next = Delete(head.next, position-1);
return head;
}

Solution:
/**
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Given linked list -- head = [4,5,1,9], which looks like following:
4 -> 5 -> 1 -> 9
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.
Note:
The linked list will have at least two elements.
All of the nodes' values will be unique.
The given node will not be the tail and it will always be a valid node of the linked list.
Do not return anything from your function.
*/
class ListNode{
int val;
ListNode next;
ListNode(int v){
val = v;
}
}
public class LinkedListDeleteNode{
/**
-we set the value of given node to the value of next node
-then we set curnode.next =curnode.next.next
*/
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
public static void main(String args[]){
ListNode root = new ListNode(1);
ListNode two = new ListNode(2);
ListNode three = new ListNode(3);
ListNode four = new ListNode(4);
ListNode five = new ListNode(5);
root.next = two;
two.next = three;
three.next = four;
four.next = five;
}
}

No comments:

Post a Comment