Singly Linked List
import java.util.*;
public class Main {
//Detect Loop in a linked linst(singly)
//define the linked list data structure
public static class node{
static int val;
static node next;
node(int val1){
node.val = val1;
node.next = null;
}
node(int val2, node next1){
node.val = val2;
node.next = next1;
}
}
public static node convertArrayToLinkedList(int[] arr){
node head = new node(arr[0]);
node prev = head;
for(int i =1; i<arr.length; i++){
node temp = new node(arr[i]);
prev.next = temp;
prev = temp;
}
return prev;
}
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
node head = convertArrayToLinkedList(arr);
node temp = head;
while(temp!=null){
System.out.println(temp.val);
temp = temp.next;
}
}
}