my solution 100% faster :)
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        ListNode* current=head;
        int counter=0;
        while(current!=nullptr){
            current=current->next;
            counter+=1;
        }
        int counter2=0;
        current=head;
        while(counter2<counter/2){
            current=current->next;
            counter2+=1;
        }
        return current;
        
    }
};
Comments (0)