Skip to content

Commit

Permalink
Problem Solving
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaspundpal committed Sep 3, 2023
1 parent 5592a01 commit caf2311
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 30 deletions.
40 changes: 38 additions & 2 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 28 additions & 28 deletions LinkedList/Delete Nth node from end.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
//This is method to delete Nth node from end of the linked list.
//if method cannot have access to head then you have to pass head to function.Here I am not passing head as my linked list having access to head.

public void deleteNthFromEnd(int n){
if(head == null){
System.out.println("List is Empty.");
return;
}
if(head.next == null){
head = null;
return;
}
int size = 0;
Node curr = head;
while(curr != null){
curr = curr.next;
size++;
}
if(n == size){
head = head.next;
return;
}
int searchIndex = (size-n)+1;
Node prev = head;
int i = 1;
while(i < searchIndex-1){
prev = prev.next;
i++;
}
prev.next = prev.next.next;
}
//public void deleteNthFromEnd(int n){
// if(head == null){
// System.out.println("List is Empty.");
// return;
// }
// if(head.next == null){
// head = null;
// return;
// }
// int size = 0;
// Node curr = head;
// while(curr != null){
// curr = curr.next;
// size++;
// }
// if(n == size){
// head = head.next;
// return;
// }
// int searchIndex = (size-n)+1;
// Node prev = head;
// int i = 1;
// while(i < searchIndex-1){
// prev = prev.next;
// i++;
// }
// prev.next = prev.next.next;
// }

0 comments on commit caf2311

Please sign in to comment.