From 9724d09396f7521386db985b884c10fbced443ef Mon Sep 17 00:00:00 2001 From: Aditya Yaduvanshi <38696088+adityayaduvanshi@users.noreply.github.com> Date: Sun, 7 Aug 2022 01:38:00 +0530 Subject: [PATCH] Update delete-the-Kth-node-from-the-end.java --- .../delete-the-Kth-node-from-the-end.java | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/algorithms/Java/linked-lists/delete-the-Kth-node-from-the-end.java b/algorithms/Java/linked-lists/delete-the-Kth-node-from-the-end.java index f658a8ab..5688a1f8 100644 --- a/algorithms/Java/linked-lists/delete-the-Kth-node-from-the-end.java +++ b/algorithms/Java/linked-lists/delete-the-Kth-node-from-the-end.java @@ -1,11 +1,4 @@ -/* Given a linked list consisting of N nodes and an integer K, your task is to delete the Kth node from the end of the linked - list */ -// Time Complexity: O(n). -// Space complexity: O(1). - -import java.util.*; - import java.io.*; class LinkedList { @@ -27,10 +20,14 @@ import java.util.*; head = new_node; } + //this function will delete the kth node in list void deleteNode(int position) { + + //if linked list is empty then return if (head == null) return; + Node temp = head; if (position == 0) { @@ -57,23 +54,20 @@ import java.util.*; } public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int t= sc.nextInt(); - while(t-- >0 ){ - int n = sc.nextInt(); - int k = sc.nextInt(); - LinkedList llist = new LinkedList(); - int p[]=new int[n]; - for(int i=0;i=0;i--) { llist.push(p[i]); } - + //this will delete the kth node llist.deleteNode(k-1); + //this will print the linked list after deleteion llist.printList(); } } }