Saturday, May 21, 2016

Find nth node from last in a linked list

Approach 1:

STEP 1:  Take 2 pointers, say ptrA and ptrB. initialize both pointers pointing at start node.

STEP 2:  Move ptrB to Nth node from start keeping ptrA at start node. 


STEP 3:
  If Nth position is not available that is, if ptrB encounters NULL before reaching Nth position, then given Nth position is not available and is incorrect, so return -1.
                 
                 If Nth position is valid input, then place ptr2 at Nth position.

STEP 4:   Now, when both pointers are at N distance from each other, increment ptrA and ptrB one node at a time until ptrB encounters NULL.


STEP 5:   When ptrB reaches NULL, it means ptrA reached Nth position from last.
 So return node at position ptrA.



public int NthElementfromLastSecondApproach(int nodeFromLast){
           Node ptr1=this.head;
           Node ptr2=this.head;
           // Return -1 if position is -ve or head is null
           if(nodeFromLast<=0 || this.head==null){
            return -1;
        }
           /* Move ptr2 to the nth element. Then move both pointers one by one,
           once ptr2 points to null, ptr1 points to the nth element from last */
           for(int i=0;i<nodeFromLast;i++){
                if(ptr2== null)
                     return -1;
                ptr2=ptr2.getNextNode();
           } // Now ptr2 pointing to nth element
          
           // Moving both elements one by one...
           while(ptr2 !=null){
                ptr2=ptr2.getNextNode();
                ptr1=ptr1.getNextNode();
           }
           return ptr1.getData();
}

Approach 2:

STEP 1: Find length of the list using length()
STEP 2: To find nth node from last, nthNode=length – n+1
STEP 3:Simply iterate and find the nthNode position which is nth node from last.

     void NthElementFromLast(int n) {
           int length = length();
           int nthNode = length - n + 1;
           Node node = find(nthNode);
           System.out.println(node);
     }

     public Node find(int position) {
           Node current = this.head;
           int count = 1;
           while (count != position) {
                current = current.getNextNode();
                count++;
           }
           return current;
         }

Running the program from main()- LinkedList.java

public class LinkedList {
     static Node head;

     public static void main(String[] args) {
           LinkedList linkedList = new LinkedList();
           linkedList.add(10);
           linkedList.add(20);
           linkedList.add(30);
           linkedList.add(40);
           linkedList.add(50);
           linkedList.add(60);
           linkedList.add(70);
           System.out.println(linkedList);
           //Node reverLinkedList = linkedList.reverseLinkedList();
           linkedList.NthElementFromLast(6);
           System.out.println(linkedList.NthElementfromLastSecondApproach(6));  }
}

Output:
{10-->20-->30-->40-->50-->60-->70-->null}
20

20

No comments:

Post a Comment