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:

No comments:

Post a Comment