任重而道远
面向代码,春暖花开
给定一个带头结点head的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。
示例:输入:[1,2,3,4,5] 则返回3
输入:[1,2,3,4,5,6] 则返回第二个结点4
typedef struct ListNode Node;
struct ListNode* middleNode(struct ListNode* head){
Node* pFast = head;
Node* pSlow = head;
while(pFast && pFast->next){
pFast = pFast->next->next;
pSlow = pSlow->next;
}
return pSlow;
}
~bye~