Skip to content

Commit

Permalink
added approach 2 of 6 in java
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Dec 28, 2018
1 parent d11ff70 commit 03c8834
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 1 deletion.
162 changes: 162 additions & 0 deletions Java/Data-Structures/LISTS/MISC/Nth-node-from-end/Approach2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import java.util.*;
abstract class Approach2
{
/**
* LINKED LIST CLASS
**/
private static class LinkedList
{
/**
* NODE CLASS
* for linked list node
**/
private class Node
{
private int data;
private Node next;

//constructor
public Node(int data)
{
this.data=data;
next=null;
}

/**
* getData()
* @return data in the node
**/
public int getData()
{
return data;
}

/**
* getNext()
* @return the next node
**/
public Node getNext()
{
return next;
}

/**
* setNext()
* @param next
**/
public void setNext(Node next)
{
this.next=next;
}
}

private Node head; //head of the linked list

//constructor for linked list
public LinkedList()
{
head=null;
}

/**
* insert_at_head()
* it will insert node at the head of the linked list
* @param data
**/
public void insert_at_head(int data)
{
Node newNode = new Node(data);
if(head==null)
{
head=newNode;
}
else
{
newNode.setNext(head);
head=newNode;
}
}

/**
* print_list()
* it will print the linked list
**/
public void printList()
{
if(head==null)
{
System.out.println("NULL");
}
else
{
Node current=head;
while(current!=null)
{
System.out.print(current.getData() +" -> ");
current=current.getNext();
}
System.out.println("NULL");
}
}

/*====================================================================*/
/**
* APPROACH 2
* Using two current pointers
* Find the remaining nodes from right on every node
* Find the count of remaining nodes
* if count is equal to the nth node to be founded then print that node
* if count is less than the nth node return
* if count is grater than the nth node then move current forward
**/
public void printNthNodeFromLast(int num)
{
Node current=head;
while(current!=null)
{
int count=0; //count of remaining node
Node current2=current;
while(current2!=null)
{
count++;
current2=current2.getNext();
}

if(count==num)
{
System.out.println(current.getData());
return;
}
else if(count<num)
{
System.out.println("fewer number of nodes");
return;
}
else if(count>num)
{
current=current.getNext();
}
}
}
/*====================================================================*/
}

public static void main(String[] args)
{
LinkedList ll = new LinkedList();
ll.insert_at_head(3);
ll.insert_at_head(1);
ll.insert_at_head(4);
ll.insert_at_head(2);
ll.insert_at_head(5);

ll.printList();

Scanner in = new Scanner(System.in);
System.out.println("Enter the nth node from last");
int n = in.nextInt();

ll.printNthNodeFromLast(n);
in.close();
}
}
2 changes: 1 addition & 1 deletion Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* MISC
* Find the nth node from end in single linked list
* APPROACH 1: [Compute the size while adding](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach1.java)
* APPROACH 2: Using two current pointers
* APPROACH 2: [Using two current pointers](Data-Structures/LISTS/MISC/Nth-node-from-end/Approach2.java)
* APPROACH 3: Using hashtable
* APPROACH 4: Using Hashtable while adding
* APPROACH 5: Finding node in one scan
Expand Down

0 comments on commit 03c8834

Please sign in to comment.