-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiddleList.java
More file actions
54 lines (41 loc) · 845 Bytes
/
MiddleList.java
File metadata and controls
54 lines (41 loc) · 845 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class MiddleList {
public static void main(String[] args){
Node head = new Node(1);
Node second1 = new Node(2);
Node second2 = new Node(3);
Node second3 = new Node(4);
Node second4 = new Node(5);
Node second5 = new Node(6);
Node second6 = new Node(7);
head.next = second1;
second1.next = second2;
second2.next = second3;
second3.next = second4;
second4.next = second5;
second5.next = second6;
System.out.println(getMiddle(null));
}
public static Node getMiddle(Node head){
if ((head ==null)||(head.next == null)){
return head;
}
Node a = head;
Node b = head;
while(a.next != null){
a=a.next.next;
if(a == null){
break;
}
b=b.next;
}
return b;
}
}
class Node{
Node next;
int value;
Node(int a){
this.value = a;
this.next = null;
}
}