File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes get fifth element from last linked list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic

get fifth element from last linked list

Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
Could anyone help me with a method for a Singly Linked List which gets fifth element from last linked list

I have the logic here:

1) have two ptrs. F_Ptr & S_Ptr.
2) Let the ptrs point to the START.
3) Take n as the offset.
4) Start traversing the S_Ptr after the F_Ptr after n.

suppose you want to find 5 element from the last of the list.

In the above case.

1) n = 5 // 5th element from the last
2) check atleast this many elements are there in the list before beginging

Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Please do not post the same question more than once. It causes confusion and duplication of effort as the community tries to help everyone.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: get fifth element from last linked list
It's not a secret anymore!