Last active
November 24, 2015 21:07
-
-
Save dhoss/717d2c2367b5b46082e2 to your computer and use it in GitHub Desktop.
foo.bar
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
import java.util.*; | |
class Main { | |
public static void main(String[] args) { | |
int[] nums1 = new int[] { | |
1, 2, 1 | |
}; | |
int[] nums2 = new int[] { | |
1, 0 | |
}; | |
int[] nums3 = new int[] { | |
1, 3, 0, 1 | |
}; | |
int[] nums4 = new int[] { | |
1, 3, 0, 1, 2, 1, 0, 4, 2 | |
}; | |
answer(nums1); | |
answer(nums2); | |
answer(nums3); | |
answer(nums4); | |
} | |
public static int answer(int[] numbers) { | |
LinkedList linkedList = new LinkedList(); | |
LinkedList.Node node; | |
for (int i = 0; i < numbers.length; i++) { | |
if (numbers[i] != 0) { | |
node = new LinkedList.Node(numbers[i]); | |
linkedList.appendIntoTail(node); | |
} | |
} | |
System.out.println("pirates " + linkedList); | |
if (linkedList.isCyclic()) { | |
System.out.println("Linked List is cyclic as it contains cycles or loop"); | |
} else { | |
System.out.println("LinkedList is not cyclic, no loop or cycle found"); | |
} | |
return 0; | |
} | |
public static class LinkedList { | |
private static Node head; | |
public LinkedList() { | |
this.head = new Node(0); | |
} | |
public Node head() { | |
return head; | |
} | |
public static void appendIntoTail(Node node) { | |
Node current = head; | |
//find last element of LinkedList i.e. tail | |
while (current.next() != null) { | |
current = current.next(); | |
} | |
System.out.println("APPENDING " + node + " TO " + current); | |
//appending new node to tail in LinkedList | |
current.setNext(node); | |
} | |
/* | |
* If singly LinkedList contains Cycle then following would be true | |
* 1) slow and fast will point to same node i.e. they meet | |
* On the other hand if fast will point to null or next node of | |
* fast will point to null then LinkedList does not contains cycle. | |
*/ | |
public static boolean isCyclic() { | |
Node fast = head; | |
Node slow = head; | |
while (fast != null && fast.next != null) { | |
fast = fast.next.next; | |
slow = slow.next; | |
System.out.println("FAST " + fast); | |
System.out.println("SLOW " + slow); | |
//if fast and slow pointers are meeting then LinkedList is cyclic | |
if (fast == slow) { | |
return true; | |
} | |
} | |
return false; | |
} | |
@Override | |
public String toString() { | |
StringBuilder sb = new StringBuilder(); | |
Node current = head.next(); | |
while (current != null) { | |
sb.append(current).append("-->"); | |
current = current.next(); | |
} | |
sb.delete(sb.length() - 3, sb.length()); // to remove --> from last node | |
return sb.toString(); | |
} | |
public static class Node { | |
private Node next; | |
private Integer data; | |
public Node(Integer data) { | |
this.data = data; | |
} | |
public Integer data() { | |
return data; | |
} | |
public void setData(Integer data) { | |
this.data = data; | |
} | |
public Node next() { | |
return next; | |
} | |
public void setNext(Node next) { | |
this.next = next; | |
} | |
@Override | |
public String toString() { | |
return Integer.toString(this.data); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment