Find the middle element of a linked list

June 23, 2022 · 1 min read

https://leetcode.com/problems/middle-of-the-linked-list/

class Solution {
public:
  ListNode *middleNode(ListNode *head) {
    ListNode *slow = head;
    ListNode *fast = head;

    while (fast != nullptr && fast->next != nullptr) {
      slow = slow->next;
      fast = fast->next->next;
    }

    return slow;
  }
};
Quicksort for linked lists
Check if a linked list is a circular linked list