Skip to content

Instantly share code, notes, and snippets.

@nguyyentantai
Created April 8, 2020 12:52
Show Gist options
  • Save nguyyentantai/49b5ef97fc199e114dadef1a40b292aa to your computer and use it in GitHub Desktop.
Save nguyyentantai/49b5ef97fc199e114dadef1a40b292aa to your computer and use it in GitHub Desktop.
Find Middle of Linked List Leetcode
/**
* 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