-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5592a01
commit caf2311
Showing
2 changed files
with
66 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
// } |