-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmiddleofll.java
More file actions
31 lines (27 loc) · 808 Bytes
/
middleofll.java
File metadata and controls
31 lines (27 loc) · 808 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
import java.util.*;
public class middleofll {
public static void main(String[] args){
middleofll obj = new middleofll();
ListNode head = obj.generateLL(new int[] {1,2,3,4,5,6});
ListNode mid = obj.findmid(head);
System.out.println(mid.val);
}
private ListNode findmid(ListNode head){
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
public ListNode generateLL(int[] nums){
ListNode Dummy = new ListNode(0);
ListNode move = Dummy;
for(int num : nums){
move.next = new ListNode(num);
move = move.next;
}
return Dummy.next;
}
}