-
Notifications
You must be signed in to change notification settings - Fork 3
/
FS(A1-Batch)D19P2.java
61 lines (54 loc) · 1.37 KB
/
FS(A1-Batch)D19P2.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
/*
Raj set up lighting for Christmas celebrations. The lighting bulbs are arranged
in a sequence, each bulb is marked by a unique number and represented in the
form of a linked list. Given a list of bulbs (root node of the linked list),
your task is to help Raj find the bulb which is at the middle of the list. If
there are two bulbs at the middle, return the latter one.
Sample test case
case=1
input=1(head) -> 2 -> 5 -> 6 -> 8
output=5
case=2
input=1(head) -> 3 -> 4 -> 7 -> 8 ->9
output=7
class ListNode
{
int val;
ListNode next;
ListNode() {}
ListNode(int val)
{
this.val = val;
}
ListNode(int val, ListNode next)
{
this.val = val;
this.next = next;
}
}
*/
import java.util.*;
class Solution
{
public void centreNode(ListNode head) {
//Implement Your Code here/*
/*
List<Integer> al = new ArrayList<>();
ListNode temp = head;
while(temp!=null){
al.add(temp.val);
temp = temp.next;
}
int res = al.size()/2;
System.out.println(al.get(res));
*/
//Two Pointer method(slow-fast method)
ListNode slow = head;
ListNode fast = head;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
}
System.out.println(slow.val);
}
}