1. 题目

2. 思路
在遍历单链表过程中,尾指针+2,中指针+1
3. 代码
/**
* 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 *r = head->next, *m = head->next;
if(m == 0) return head;
while(r != 0){
r = r->next;
if(r != 0){
r = r->next;
if(r != 0){
m = m->next;
}
}
}
return m;
}
};
1万+


&spm=1001.2101.3001.11974&articleId=105060038&d=1&t=3&u=4f9a49addd914c7ab5ee782952f40308)

被折叠的 条评论
为什么被折叠?



