-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2. Find nth node from the end of the single link list 3. Loop detection in the single link list
- Loading branch information
Showing
10 changed files
with
561 additions
and
3 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
Java/src/com/godepth/FindNodeFromEndLinklist/FindNodeEfficient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.godepth.FindNodeFromEndLinklist; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class FindNodeEfficient<T> { | ||
|
||
private LinkedList<Node<T>> linkList = null; | ||
private Node<T> nthNode = null; | ||
private Node<T> pTemp = null; | ||
|
||
public FindNodeEfficient(List<T> arrayList) | ||
{ | ||
this.linkList = new LinkedList<>(); | ||
|
||
for(T element : arrayList) { | ||
Node<T> node = new Node<>(); | ||
node.setData(element); | ||
this.linkList.add(node); | ||
} | ||
} | ||
|
||
public void find(int position) | ||
{ | ||
int count = 0; | ||
int tempCount = 0; | ||
|
||
for (Node<T> i : this.linkList) { | ||
System.out.println(i.getData()); | ||
} | ||
|
||
this.pTemp = this.linkList.getFirst(); | ||
|
||
Node<T> last = this.linkList.getLast(); | ||
|
||
while (this.pTemp != last) { | ||
if (count < position - 1) { | ||
this.pTemp = this.linkList.get(count); | ||
} else { | ||
this.nthNode = this.linkList.get(tempCount); | ||
this.pTemp = this.linkList.get(count); | ||
tempCount++; | ||
} | ||
count++; | ||
} | ||
|
||
if (this.nthNode == null) { | ||
System.out.println("Cannot find the " + position + "th node from the end of the link list!"); | ||
} else { | ||
System.out.println("The " + position + "th node data from the end of the link list is " + this.nthNode.getData()); | ||
} | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
Java/src/com/godepth/FindNodeFromEndLinklist/FindNodeHashTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.godepth.FindNodeFromEndLinklist; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class FindNodeHashTable<T> { | ||
|
||
private LinkedList<Node<T>> linkList = new LinkedList<>(); | ||
|
||
public FindNodeHashTable(List<T> list) | ||
{ | ||
for (T i : list) { | ||
Node<T> node = new Node<>(); | ||
node.setData(i); | ||
this.linkList.add(node); | ||
} | ||
} | ||
|
||
public void find(int position) { | ||
|
||
int size = this.linkList.size(); | ||
|
||
int target = size - position; | ||
|
||
System.out.println("The " + position + "th node from the end of the link list is '" + this.linkList.get(target).getData() + "'"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.godepth.FindNodeFromEndLinklist; | ||
|
||
public class Node<T> implements Comparable<T> { | ||
private T data; | ||
|
||
public T getData() | ||
{ | ||
return this.data; | ||
} | ||
|
||
public void setData(T data) | ||
{ | ||
this.data = data; | ||
} | ||
|
||
@Override | ||
public int compareTo(T data) | ||
{ | ||
return this.getData() == data ? 0 : 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.godepth.LoopLinkedList; | ||
|
||
import com.godepth.FindNodeFromEndLinklist.FindNodeHashTable; | ||
|
||
import java.util.List; | ||
|
||
public class DetectLoop<T> { | ||
|
||
private LoopLinkedList<T> loopLinkedList = null; | ||
|
||
private Node<T> harePointer = null; | ||
|
||
private Node<T> tortisePointer = null; | ||
|
||
public DetectLoop(List<T> list, int loopFrom) | ||
{ | ||
this.loopLinkedList = new LoopLinkedList<>(); | ||
|
||
for (T i : list) { | ||
this.loopLinkedList.push(i); | ||
} | ||
|
||
//Create the loop | ||
this.loopLinkedList.makeLoop(2); | ||
} | ||
|
||
public void find() | ||
{ | ||
if (this.loopLinkedList != null) { | ||
Node<T> current = this.loopLinkedList.getHead(); | ||
|
||
// while (current != null) { | ||
// System.out.println(current.getData()); | ||
// current = current.getNext(); | ||
// } | ||
|
||
this.tortisePointer = current; | ||
this.harePointer = current.getNext(); | ||
boolean flag = false; | ||
|
||
while (true) { | ||
|
||
if (this.harePointer == this.tortisePointer) { | ||
flag = true; | ||
break; | ||
} else { | ||
if (this.tortisePointer.getNext() != null) { | ||
this.tortisePointer = this.tortisePointer.getNext(); | ||
} else { | ||
flag = false; | ||
break; | ||
} | ||
|
||
if (this.harePointer.getNext() != null && this.harePointer.getNext().getNext() != null) { | ||
this.harePointer = this.harePointer.getNext().getNext(); | ||
} else { | ||
flag = false; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (!flag) { | ||
System.out.println("Loop not detected!"); | ||
} else { | ||
System.out.println("Loop detected!"); | ||
} | ||
|
||
} else { | ||
System.out.println("The link list is empty."); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.godepth.LoopLinkedList; | ||
|
||
|
||
import java.util.List; | ||
|
||
public class LoopLinkedList<T> { | ||
|
||
private Node<T> head = null; | ||
private Node<T> tail = null; | ||
private Node<T> previous = null; | ||
|
||
public void push(T element) | ||
{ | ||
Node<T> newNode = new Node<>(); | ||
newNode.setData(element); | ||
|
||
if (this.previous == null) { | ||
this.head = newNode; | ||
} else { | ||
this.previous.setNext(newNode); | ||
} | ||
|
||
this.previous = newNode; | ||
|
||
this.tail = newNode; | ||
} | ||
|
||
public Node<T> getHead() | ||
{ | ||
return this.head; | ||
} | ||
|
||
public Node<T> getTail() | ||
{ | ||
return this.tail; | ||
} | ||
|
||
public void makeLoop(int position) | ||
{ | ||
Node<T> node = this.head.getNext(); | ||
int count = 0; | ||
|
||
while (node != null) { | ||
node = node.getNext(); | ||
if (count == position) { | ||
this.tail.setNext(node); | ||
break; | ||
} | ||
count++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.godepth.LoopLinkedList; | ||
|
||
public class Node<T> implements Comparable<T> { | ||
|
||
private T data; | ||
private Node<T> next = null; | ||
|
||
public T getData() | ||
{ | ||
return this.data; | ||
} | ||
|
||
public void setData(T data) | ||
{ | ||
this.data = data; | ||
} | ||
|
||
public Node<T> getNext() | ||
{ | ||
return this.next; | ||
} | ||
|
||
public void setNext(Node<T> node) | ||
{ | ||
this.next = node; | ||
} | ||
|
||
@Override | ||
public int compareTo(T data) | ||
{ | ||
return this.getData() == data ? 0 : 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.