How to find nth node from the end of a Singly Linked List in Java?

  Рет қаралды 33,923

Dinesh Varyani

Dinesh Varyani

Күн бұрын

Пікірлер: 57
@itsdineshvaryani
@itsdineshvaryani 4 жыл бұрын
Please *Like* , *Comment* , *Share* , *Subscribe* and *Click Bell* 🔔🔔🔔 Icon for More Updates. To get *Data Structures and Algorithms* complete course for free please follow this link - kzbin.info/aero/PL6Zs6LgrJj3tDXv8a_elC6eT_4R5gfX4d
@shubhamagarwal1434
@shubhamagarwal1434 2 жыл бұрын
Best series for DS & Algo. I am also 10 yrs java exp guy. Was looking for DS & Algo free course over KZbin with java implementation and found this. Hats Off To You Man...Excellent Work. GOD BLESS YOU :)
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
Thanks !!!
@AshwaniKumar-fp3nk
@AshwaniKumar-fp3nk 3 жыл бұрын
Hands down, that's the best explanation ever.
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Thanks !!!
@kritisingh3194
@kritisingh3194 3 жыл бұрын
My approach( O(n) ): int getNthFromLast(Node head, int n) { Node current = head; int len = 0; while (current!=null) { current=current.next; len++; } if(head == null || n>len) { return -1; } int count = 0; current = head; while (count < (len - n)) { current = current.next; count++; } return current.data; }
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
cool !!!
@codingc3684
@codingc3684 Жыл бұрын
bhut vdia yr 🙃
@keerthigeetha4182
@keerthigeetha4182 5 ай бұрын
your teaching style is excellent make more videos on different subjects the best you tube channel I have ever seen 🎉🎉
@itsdineshvaryani
@itsdineshvaryani 5 ай бұрын
Thanks
@arpitamandal6469
@arpitamandal6469 3 жыл бұрын
simplest explanation .. thanks for making it so simple
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Welcome !!!
@danielsenik2432
@danielsenik2432 3 жыл бұрын
You can also use your code from previous video(find nth node), just pass as parameter list.size - n
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
didnt get you !!!
@sathishkumarr7106
@sathishkumarr7106 2 жыл бұрын
@@itsdineshvaryani First find the length of the List. Just traverse from head to till (length - n).
@arkojyotibhattacharya9302
@arkojyotibhattacharya9302 2 жыл бұрын
Absolutely Phenomenal. Thank you, sir🙏
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
Welcome !!
@meetshah4432
@meetshah4432 3 жыл бұрын
Hello sir, love your videos, they teach me a lot. But I have a question, why don't you ever use for loop? In most of the algorithms that I saw in this series, for loop makes the code way shorter. So can you please tell me why do you avoid using for loop?
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
I have used it ... there is no such preference ...
@namankabadi2792
@namankabadi2792 Жыл бұрын
Sir your videos are very nice.. Sir it will be best if u could add time complexity for the algorithm you are explaining..
@itsdineshvaryani
@itsdineshvaryani Жыл бұрын
Will try
@mitigateddrip6129
@mitigateddrip6129 2 жыл бұрын
If n is equal to the number of nodes in linked list then it will give the right answer but it will be throwing exception at same time
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
You can correct code by adding that edge case ... Ok ???
@mpbe
@mpbe 3 жыл бұрын
sir you are best...
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
thanks !!!
@poojithagampa4315
@poojithagampa4315 Жыл бұрын
is this code work for even length of list sir
@surajmahato2557
@surajmahato2557 3 жыл бұрын
Wow what an explaination👏
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Thanks !!!
@garimakhare15
@garimakhare15 3 жыл бұрын
Thank yo so much for uploading this video
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Welcome !!! There is complete playlist in the description of video !!!
@juliemoore728
@juliemoore728 7 жыл бұрын
Excellent!
@itsdineshvaryani
@itsdineshvaryani 7 жыл бұрын
Julie Moore Thanks !!!
@shashikantmaurya1038
@shashikantmaurya1038 3 жыл бұрын
sir there is issue with this algorithm suppose last three node value is need to be addressed but there is only two element in linkedlist so the count method to take the reference pointer ahead will give nullpointer Exception.
@nupursurbhi3987
@nupursurbhi3987 3 жыл бұрын
yes that corner case needs to be addressed.
@rajarishabhsingh8296
@rajarishabhsingh8296 2 жыл бұрын
Tussi great ho sir ji!!
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
Thanks !!!
@leoking2386
@leoking2386 3 жыл бұрын
for n = 0 code gives exception. while (count < position) { count++; refPointer = refPointer.next; } while (refPointer.next != null) { refPointer = refPointer.next; mainPointer = mainPointer.next; } if (mainPointer.next == null) { return mainPointer; } return mainPointer.next; }
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
ok
@ananthukkumar987
@ananthukkumar987 4 ай бұрын
Wow, Intelligent algorithm
@marals1061
@marals1061 6 жыл бұрын
Thank you
@vedantmahajan558
@vedantmahajan558 3 жыл бұрын
awesome content
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Thanks. I request you to share it with ur friends and colleagues.
@Ankit-we8ym
@Ankit-we8ym 5 жыл бұрын
why this work ,sir please explain anyone?
@deliveringIdeas
@deliveringIdeas 3 жыл бұрын
O(n^2) solution..? Not good, right?
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
O(n)
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
O(n)
@deliveringIdeas
@deliveringIdeas 3 жыл бұрын
@@itsdineshvaryani two while-loops would be O(n^2) right? (Two traversals)
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
@@deliveringIdeas wrong ... Its O(n)
@nagabhushanr5599
@nagabhushanr5599 3 жыл бұрын
@@deliveringIdeas loops are not within one another so its not O(n^2)
@arijitmohapatra147
@arijitmohapatra147 3 жыл бұрын
Sir can't we get the source code ?
@itsdineshvaryani
@itsdineshvaryani 3 жыл бұрын
Its in description of video !!! Pls check !!!
@arijitmohapatra147
@arijitmohapatra147 3 жыл бұрын
@@itsdineshvaryani ty sir
@davidfield2030
@davidfield2030 2 жыл бұрын
The video should be renamed: How to find the nth node from the START of a Singly Linked List
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
nope !!!
@vikasbalani6378
@vikasbalani6378 2 жыл бұрын
Nope. He has created two pointers actually. One (refPtr) started early and from head node to count 'n' ,then another pointer (mainPtr) will start along with refPtr. So once refPtr ends its journey then mainPtr would be nth position before the end of linkedlist.
@itsdineshvaryani
@itsdineshvaryani 2 жыл бұрын
@@vikasbalani6378 thanks !!!
@wakeelmalik8387
@wakeelmalik8387 2 жыл бұрын
if newNode.data=0 , then your code throwing NullPointerException
Why Floyd's Cycle Detection algorithm works?
19:30
Dinesh Varyani
Рет қаралды 21 М.
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 167 МЛН
كم بصير عمركم عام ٢٠٢٥😍 #shorts #hasanandnour
00:27
hasan and nour shorts
Рет қаралды 8 МЛН
How to remove duplicate from sorted Singy Linked List in Java?
11:09
Dinesh Varyani
Рет қаралды 33 М.
Insert a node in a Singly Linked List at a given position (Animation)
15:33
Recursive PreOrder traversal of a Binary Tree in Java
26:06
Dinesh Varyani
Рет қаралды 61 М.
SQLModel + FastAPI: Say Goodbye to Repetitive Database Code
19:50
How to remove a given key from Singly Linked List in Java?
10:34
Dinesh Varyani
Рет қаралды 28 М.
Nth Node from the end of a linked list python 3
16:30
Stack Equation
Рет қаралды 539
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 167 МЛН