1、简单做法
先求链表长度,然后求出中点,返回中间结点即可。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* middleNode(ListNode* head) {
int cnt=1;
ListNode* cru=head;
while(cru->next!=NULL)
{
cru=cru->next;
++cnt;
}
cnt/=2;
for(int i=0;i<cnt;++i) head=head->next;
return head;
}
};