Day 8 of 30-Day LeetCode Challenge

Given a non-empty, singly linked list with head node head, return a middle node of linked list.

If there are two middle nodes, return the second middle node.

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

}
};

--

--

We never really grow up, we only learn how to act in public

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store