这道题和题目描述的不符,答案给的是不含头结点的操作。。。
按不含头结点的做就可以
遍历链表,将结点都存入vector中
我的解题:
/**
* 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) {
vector<ListNode*> v;
ListNode *a=head;
while(a){
v.push_back(a);
a=a->next;
}
return v[v.size()/2];
}
};