+ Reply to Thread
Results 1 to 2 of 2

Thread: Find nth node from last of a singly linked list

  1. #1
    Krazy Guest

    Find nth node from last of a singly linked list

    Find nth node from last of a singly linked list. You need to take care all the possible cases.
    (there could be cycle in the list)

  2. #2
    arif4123 is offline Junior Member
    Join Date
    Apr 2011
    Posts
    2
    //Declare three global variables:
    Boolean found=false;
    Node requiredNthNode=null;
    Int nodeCount=0;

    //Now write a recurrsive method Process(Node)
    void Process(Node node)
    {
    if(node->next)
    Process(node->next)

    nodeCount++;
    if(nodeCount==N) //where N is the count from last
    {
    found=true;
    requiredNthNode=node;

    }

    }

    //Now the Actual Method
    Node FindNthNode(head)
    {
    Process(head);
    if(found)
    return requiredNthNode;
    else
    return NULL;
    }
    Last edited by arif4123; 30th April 2011 at 14:55.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find kth node from last in a linked list
    By Surfer in forum Motorola
    Replies: 1
    Last Post: 24th November 2010, 12:46
  2. Delete alternate node from a Linked List
    By TopGun in forum Amazon
    Replies: 0
    Last Post: 30th May 2008, 11:56
  3. Find nth Node from end in a Linked List
    By TopGun in forum Linked Lists
    Replies: 0
    Last Post: 17th February 2008, 23:05

Tags for this Thread

Bookmarks

Posting Permissions

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