Skip to content

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.

Example 1:

img

Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.

Example 2:

img

Input: head = [1,2,3,4,5,6]
Output: [4,5,6]
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.

Constraints:

  • The number of nodes in the list is in the range [1, 100].
  • 1 <= Node.val <= 100

Solution

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        // base case 
        if (head == null || head.next == null){
            return head;
        }

        ListNode fast = head;
        ListNode slow = head;

        while(fast.next != null && fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        if (fast.next == null) {
            // odd linked list length
            return slow;
        }else {
            // even linked list length
            return slow.next;
        }

    }
}
// TC: O(n)
// SC: O(1)

/* 

         1 -> 2 -> 3 -> 4 
                   f      ->  f.next.next = null -> stop 
              s                      -> return s.next



         1-> 2 -> 3 -> 4 -> 5       -> f.next = nulll -> stop
                            f     
                  s                  -> return s




*/