示例:
输入:[1,2,3,4,5]
输出:此列表中的结点 3
输入:[1,2,3,4,5,6]
输出:此列表中的结点 4
解题思路:
快慢指针法,让快指针走两步慢指针走一步,当个数为奇数时,
fast->next==NULL,当为偶数时fast == NULL。
typedef struct ListNode Node;
struct ListNode* middleNode(struct ListNode* head){
Node* slow = head;
Node* fast = head;
while(fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
来源:CSDN
作者:smilevampire
链接:https://blog.csdn.net/smilevampire/article/details/104071222