-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleLinkedList.java
More file actions
60 lines (54 loc) · 1.01 KB
/
middleLinkedList.java
File metadata and controls
60 lines (54 loc) · 1.01 KB
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
import java.util.LinkedList;
public class middleLinkedList {
public static void main(String[] args) {
// TODO Auto-generated method stub
Node root=new Node(10);
Node n1= new Node(20);
Node n2= new Node(30);
Node n3 = new Node(40);
Node n4=new Node(50);
Node n5= new Node(60);
Node n6= new Node(70);
Node n7=new Node(80);
root.next=n1;
n1.next=n2;
n2.next=n3;
n3.next=n4;
n4.next=n5;
n5.next=n6;
n6.next=n7;
n7.next=null;
int j=0,data=root.data;
double i=0;
Node head=new Node(root.data);
head=root;
while(root!=null) {
j=j+1;
i=i+0.5;
data=root.data;
root=root.next;
}
int midData=head.data;
int a=0;
for( a=0;a<i;a++) {
midData=head.data;
head=head.next;
}
System.out.println("the length of linked list is "+j+" with value "+data);
System.out.println("the middle of linked list is "+a+ " with value "+midData);
}
}
class Node{
int data;
Node next;
Node(){
}
Node(int d){
data =d;
next=null;
}
Node(int d,Node next){
data =d;
this.next=next;
}
}