Saturday, March 11, 2017

Find Middle in LinkedList

public ListNode findMiddle(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode slow = head;
ListNode fast = head;
while (slow.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;

}

No comments:

Post a Comment