Skip to content

Commit

Permalink
1. Double link list implementation
Browse files Browse the repository at this point in the history
2. Find nth node from the end of the single link list
3. Loop detection in the single link list
  • Loading branch information
targarace committed Jan 3, 2018
1 parent c5dd572 commit 307549c
Show file tree
Hide file tree
Showing 10 changed files with 561 additions and 3 deletions.
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());
}
}

}
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() + "'");
}
}
21 changes: 21 additions & 0 deletions Java/src/com/godepth/FindNodeFromEndLinklist/Node.java
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;
}
}
75 changes: 75 additions & 0 deletions Java/src/com/godepth/LoopLinkedList/DetectLoop.java
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.");
}

}

}
52 changes: 52 additions & 0 deletions Java/src/com/godepth/LoopLinkedList/LoopLinkedList.java
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++;
}
}
}
33 changes: 33 additions & 0 deletions Java/src/com/godepth/LoopLinkedList/Node.java
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;
}
}
65 changes: 63 additions & 2 deletions Java/src/com/godepth/Main.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.godepth;

import com.godepth.LoopLinkedList.DetectLoop;
import com.godepth.FindNodeFromEndLinklist.FindNodeEfficient;
import com.godepth.FindNodeFromEndLinklist.FindNodeHashTable;
import com.godepth.doublelinkedlist.DoubleLinkedList;
import com.godepth.island.Island;
import com.godepth.pathfinder.Pathfinder;
import com.godepth.singlelinkedlist.SingleLinkedList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main
{
public static void main(String[] args)
{
//runIslandCounter();
//runPathFinder();
runSingleLinkedList();
//runSingleLinkedList();
//runDoubleLinkedList();
//runFindNodeFromEndEfficiently();
//runFindNodeFromEndHashTable();
runDetectLoopInLinkList();
}

private static void runIslandCounter()
Expand Down Expand Up @@ -43,7 +53,7 @@ private static void runPathFinder()

private static void runSingleLinkedList()
{
SingleLinkedList<Integer> linkedList = new SingleLinkedList<Integer>();
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();

linkedList.add(1);
linkedList.add(2);
Expand All @@ -60,4 +70,55 @@ private static void runSingleLinkedList()
linkedList.traverse();
}

private static void runDoubleLinkedList()
{
DoubleLinkedList<String> linkedList = new DoubleLinkedList<>();

linkedList.addAtStart("0.5");
linkedList.addAfter("First", "0.5");
linkedList.addAfter("Second" ,"First");
linkedList.addAfter("Third" ,"Second");
linkedList.addAfter("2.5" ,"Second");
linkedList.addAfter("Fourth", "Third");
linkedList.addAfter("4.5", "Fourth");
linkedList.removeFirst();
// linkedList.removeLast();
linkedList.remove("2.5");

linkedList.traverse();
}

private static void runFindNodeFromEndEfficiently()
{
List<Integer> list = new ArrayList<>();
Collections.addAll(list, 1,2,3,4,5,6,7,8,9,10);
FindNodeEfficient<Integer> linkList = new FindNodeEfficient<>(list);

linkList.find(9);
}

private static void runFindNodeFromEndHashTable()
{
List<String> list = new ArrayList<>();
Collections.addAll(
list,
"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"
);
FindNodeHashTable<String> linkList = new FindNodeHashTable<>(list);

linkList.find(9);
}

private static void runDetectLoopInLinkList()
{
List<String> list = new ArrayList<>();
Collections.addAll(
list,
"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"
);

DetectLoop<String> detectLoop = new DetectLoop<>(list, 3);

detectLoop.find();
}
}
Loading

0 comments on commit 307549c

Please sign in to comment.