Leetcode 876 Middle of the Linked List
struct ListNode
{
int val;
ListNode* next;
ListNode(int x):val(x),next(NULL){}
};
class Solution {
public:
ListNode* middleNode(ListNode* head) {
if(!head || !head->next)
return head;
int count = 0;
ListNode* node = head;
while(head){
head = head->next;
count ++;
}
for(int i = 0;i < count/2;++i){
node = node->next;
}
//if(count % 2 == 0)
// node = node->next;
return node;
}
};