Home Leetcode 876 Middle of the Linked List
Post
Cancel

Leetcode 876 Middle of the Linked List

Middle of the Linked List problem results

Middle of the Linked List

Result

First Blood

Saving each node in a vector, calculate the middle index, and get the node in the vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
    ListNode* middleNode(ListNode* head) {
        vector<ListNode*> node_list;
        node_list.push_back(head);
        while(head->next != nullptr)
        {
            head = head->next;
            node_list.push_back(head);
        }
        
        int index = node_list.size()/2;
       
        return node_list[index];
    }
}
This post is licensed under CC BY 4.0 by the author.
Trending Tags