Created
April 8, 2020 12:52
-
-
Save nguyyentantai/49b5ef97fc199e114dadef1a40b292aa to your computer and use it in GitHub Desktop.
Find Middle of Linked List Leetcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
ListNode* middleNode(ListNode* head) { | |
if(head==NULL|| head->next==NULL) | |
return head; | |
ListNode* slow = head; | |
ListNode* fast = head; | |
while(fast != NULL && fast->next != NULL){ | |
fast = fast->next->next; | |
slow = slow->next; | |
} | |
return slow; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment