-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMidPoint.java
More file actions
45 lines (38 loc) · 875 Bytes
/
Copy pathMidPoint.java
File metadata and controls
45 lines (38 loc) · 875 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
package com.rrohit.algo.linkedlist;
/*
* @author rrohit
*/
public class MidPoint {
public <T>Node<T> findMidNode(Node<T> head) {
if (head == null) {
System.out.println("List Empty ::");
return null;
}
Node<T> mid = head, fast = head;
while (fast.getNext() != null) {
fast = fast.getNext();
if (fast.getNext() != null) {
fast = fast.getNext();
mid = mid.getNext();
}
}
return mid;
}
public void displayReverse(Node head){
if(head == null){
return;
}
displayReverse(head.getNext());
System.out.print(head);
}
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
for (int i=1; i<5; i++) {
list.add(i);
}
list.display();
MidPoint mp = new MidPoint();
System.out.println("Mid = "+mp.findMidNode(list.getHead()));
mp.displayReverse(list.getHead());
}
}