• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nth Node from Last ???

 
Nandish Sri
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to find the N'th node from last, in a singly linked list .. ? You are given only the first node..

Any idea... ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a homework question?

Do you understand how singly linked lists work?

Without immediately thinking about how to implement this in Java, what do you think yourself would be the steps to solve this?
 
Nandish Sri
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question running in my mind.. I need the efficient logic.

my logic is... indexing the LL in an array and get the what Nth element in a shot.. But still this is inefficient . But, How to find the Nth Node from Last in a single traversal... ?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Nandish KB",

Somebody probably should have mentioned it sooner, but your name does not quite meet our Naming policy. You need a real sounding first AND last name (although initials for a first name are allowed). We'd appreciate it if you'd change it by going here.

Now, about your question...

forget about java for a minute. if i said to you "i'm going to recite some numbers. when i'm done, tell me the 13th from last one", how would you do it? think about what you need to remember, and what you can forget, and when.

write out the steps you'd take on paper, or post them here, and we'll be happy to review them with you. Once you have that, writing the code is much easier.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic