-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
40 lines (30 loc) · 818 Bytes
/
Copy pathLinkedList.java
File metadata and controls
40 lines (30 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class LinkedList {
public Node head;
public LinkedList(){
head = new Node("head");
}
public void appendToEnd(Node node){
//determine what place in the link list this node will be added
//current node is set to the beginning of the list
Node current = head;
//check to see if the next node is null or not
while(current.next != null){
current = current.next;
}
//if current.next is null then we want the node that was passed
current.next = node;
}
public boolean isCyclic(){
Node slow = head;
Node fast = head;
while(slow.next != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
//if slow node points to the fast node then there loop
if(slow == fast){
return true;
}
}
return false;
}
}