-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path876.java
More file actions
27 lines (25 loc) · 698 Bytes
/
876.java
File metadata and controls
27 lines (25 loc) · 698 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
/**
* 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.
*/
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
if (fast.next != null) fast = fast.next;
}
return slow;
}
}
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}