Middle of the Linked List

PHOTO EMBED

Thu Jun 16 2022 18:51:22 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

/**
 * 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) {
        int count=0;
        ListNode *i=head;
        
        while(i!=NULL)
        {
            count++;
            i=i->next;
        }
        for(int i=0;i<count/2;i++)
        {
            head=head->next;
        }
        return head;
    }
};
content_copyCOPY

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