• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nth Node from Last ???

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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... ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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... ?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic