似乎很简单
/**
* 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) {
ListNode *p=head;
int n=0;
while(p->next!=NULL){
p=p->next;
++n;
}
n=n+1;
cout<<n<<endl;
int i=0;
p=head;
for(;i<n/2;++i){
p=p->next;
}
return p;
}
};
执行用时 : 8 ms, 在Middle of the Linked List的C++提交中击败了86.94% 的用户
内存消耗 : 8.5 MB, 在Middle of the Linked List的C++提交中击败了18.20% 的用户