-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicsll.java
74 lines (54 loc) · 1.54 KB
/
basicsll.java
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//import java.util.*;
public class basicsll{
public static void displayReverse(Node head){ //Displaying a reverse of LL Recursively
if(head == null) return;
displayReverse(head.next);
System.out.print(head.data+ " ");
}
public static void displayR(Node head){ //Displaying a LL Recursively
if(head == null) return;
System.out.print(head.data +" ");
displayR(head.next);
}
public static void display(Node head){// DISPLAYING A lINKED lIST
Node temp = head;
while(temp != null){
System.out.print(temp.data + " ");
temp = temp.next;
}
}
public static int lengthOfLL(Node head){//Length of LinkedList
if(head == null) return 0;
int count = 0;
Node temp = head;
while(temp != null){
count +=1;
temp = temp.next;
}
return count;
}
public static class Node{
int data;
Node next;
Node (int data){
this.data = data;
}
}
public static void main(String[] args) {
Node a = new Node(1);
Node b = new Node(4);
Node c = new Node(3);
Node d = new Node(7);
a.next = b;
b.next = c;
c.next = d;
display(a);
System.out.println();
displayR(a);
System.out.println();
displayReverse(a);
System.out.println();
int length = lengthOfLL(a);
System.out.print(length);
}
}