Given a singly linked list, delete a node at the kth position without using the loop.
Examples:
Input : list = 9->8->3->5->2->1
k = 4
Output : 9->8->3->2->1
Input : list = 0->0->1->6->2->3
k = 3
Output : 0->0->6->2->3
We recursively reduce the value of k. When k reaches 1, we delete the current node and return the next current node as a new node. When the function returns, we link the returned node to the next previous node.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* deleteNode(Node* start, int k)
{
if (k < 1)
return start;
if (start == NULL)
return NULL;
if (k == 1)
{
Node *res = start->next;
delete (start);
return res;
}
start->next = deleteNode(start->next, k-1);
return start;
}
void push( struct Node **head_ref, int new_data)
{
struct Node *new_node = new Node;
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
void printList( struct Node *head)
{
while (head!=NULL)
{
cout << head->data << " " ;
head = head->next;
}
printf ( "\n" );
}
int main()
{
struct Node *head = NULL;
push(&head,3);
push(&head,2);
push(&head,6);
push(&head,5);
push(&head,11);
push(&head,10);
push(&head,15);
push(&head,12);
int k = 3;
head = deleteNode(head, k);
printf ( "\nModified Linked List: " );
printList(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node deleteNode(Node start, int k)
{
if (k < 1 )
return start;
if (start == null )
return null ;
if (k == 1 )
{
Node res = start.next;
return res;
}
start.next = deleteNode(start.next, k- 1 );
return start;
}
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void printList( Node head)
{
while (head!= null )
{
System.out.print(head.data + " " );
head = head.next;
}
System.out.printf( "\n" );
}
public static void main(String args[])
{
Node head = null ;
head=push(head, 3 );
head=push(head, 2 );
head=push(head, 6 );
head=push(head, 5 );
head=push(head, 11 );
head=push(head, 10 );
head=push(head, 15 );
head=push(head, 12 );
int k = 3 ;
head = deleteNode(head, k);
System.out.printf( "\nModified Linked List: " );
printList(head);
}
}
|
Python3
class Node( object ):
def __init__( self , d):
self .data = d
self . next = None
def deleteNode(start, k) :
if (k < 1 ) :
return start
if (start = = None ):
return None
if (k = = 1 ) :
res = start. next
return res
start. next = deleteNode(start. next , k - 1 )
return start
def push(head_ref, new_data) :
new_node = Node( 0 )
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
def printList( head) :
while (head ! = None ):
print (head.data, end = " " )
head = head. next
print ( "\n" )
head = None
head = push(head, 3 )
head = push(head, 2 )
head = push(head, 6 )
head = push(head, 5 )
head = push(head, 11 )
head = push(head, 10 )
head = push(head, 15 )
head = push(head, 12 )
k = 3
head = deleteNode(head, k)
print ( "Modified Linked List: " , end = "")
printList(head)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node deleteNode(Node start, int k)
{
if (k < 1)
return start;
if (start == null )
return null ;
if (k == 1)
{
Node res = start.next;
return res;
}
start.next = deleteNode(start.next, k-1);
return start;
}
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void printList( Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
Console.Write( "\n" );
}
public static void Main(String []args)
{
Node head = null ;
head=push(head,3);
head=push(head,2);
head=push(head,6);
head=push(head,5);
head=push(head,11);
head=push(head,10);
head=push(head,15);
head=push(head,12);
int k = 3;
head = deleteNode(head, k);
Console.Write( "\nModified Linked List: " );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function deleteNode(start , k) {
if (k < 1)
return start;
if (start == null )
return null ;
if (k == 1) {
var res = start.next;
return res;
}
start.next = deleteNode(start.next, k - 1);
return start;
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function printList(head) {
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
document.write( "\n" );
}
var head = null ;
head = push(head, 3);
head = push(head, 2);
head = push(head, 6);
head = push(head, 5);
head = push(head, 11);
head = push(head, 10);
head = push(head, 15);
head = push(head, 12);
var k = 3;
head = deleteNode(head, k);
document.write( "\nModified Linked List: " );
printList(head);
</script>
|
OutputModified Linked List: 12 15 11 5 6 2 3
Time Complexity: O(n)
Auxiliary Space: O(n)
This article is contributed by Mohd Saleem. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.