876_MiddleoftheLinkedList - a920604a/leetcode GitHub Wiki


title: 876. Middle of the Linked List tags:
- Linked List - Two Pointers categories: leetcode comments: false

problem

solution

class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        ListNode *slow = head, *fast = head;
        while(fast && fast->next ){
            fast = fast->next->next;
            slow= slow->next;
        }
        return slow;
        
    }
};

analysis

  • time complexity O(n)
  • speed complexity O(1)