Middle of the Linked List 2020-10-15 03:23
public ListNode middleNode(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode slow = dummy;
ListNode fast = dummy;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
if (fast == null) {
return slow;
}
return slow.next;
}
Runtime | Memory |
---|---|
0 ms | 36.2 MB |
EOF