Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default Java RECURSION help..finding the second last member in the List?

    For finding the last member in the list I'm using this:

    public List getLast(){
    if(this.getNext() == null)
    return this;
    else
    return this.getNext().getLast();
    }

    I dont know how to find the second last member....

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    Why don't you just use the methods List provides for you: size(), and get(). For the last element you would say listName.get(listName.size()-1); for 2nd to last: listName.get(listName.size()-2);

    Also, Instead of make a recursive method, an Iterative might be better. First, call getNaxt() until you get null with a counter. So the counter is the size of the list. Then call getNext() that many times -1 or -2 etc...

  3. #3
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default

    I have to use only Recursive method. Its the part of the HW.



    You mean like this
    public List getSecondLast(){
    if(this.getNext().getLast() == null)
    return this;
    else
    return this.get(this.getSize()-2);

    Does it matter if there is Nodes in PathNode instead of Members in the List.
    Will the code remain the same.

  4. #4
    Inennyasype's Avatar
    Inennyasype is offline Member
    Join Date
    Apr 2011
    Location
    Malawi
    Posts
    1
    Rep Power
    0

Similar Threads

  1. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  2. Finding the closest point in a list of points
    By sAntA199 in forum New To Java
    Replies: 8
    Last Post: 12-13-2009, 09:41 AM
  3. Finding the most repeated names in a list
    By jboy in forum New To Java
    Replies: 2
    Last Post: 09-17-2009, 04:08 PM
  4. Finding objects in a list
    By starwars in forum AWT / Swing
    Replies: 5
    Last Post: 09-11-2009, 04:42 PM
  5. Replies: 5
    Last Post: 02-07-2009, 08:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •