- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathMiddleOfTheLinkedList.java
43 lines (27 loc) · 839 Bytes
/
MiddleOfTheLinkedList.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
/*
LEETCODE QUESTION : 876. Middle of the Linked List
=> Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.
*/
// * Definition for singly-linked list.
publicclassListNode {
intval;
ListNodenext;
ListNode() {
}
ListNode(intval) {
this.val = val;
}
publicclassMiddle_of_the_Linked_List {
//Add code from here in Leetcode.
publicListNodemiddleNode(ListNodehead) {
ListNodefast = head;
ListNodeslow = head;
while(fast!= null && fast.next != null){
slow=slow.next;
fast= fast.next.next;
//length=lenght+1;
}
returnslow;
}
}