思路:設置兩個指針,一個指針爲fast,一個指針爲slow,fast的步長爲2,slow的步長爲1,當fast到達鏈表的最後節點時,slow指針所指向的就是鏈表的中間節點。ide
具體代碼實現:spa
1 LinkList *GetMidNode(LinkList *head) 2 { 3 LinkList *slow = head; 4 LinkList *fast = slow; 5 6 while(slow) 7 { 8 fast = fast->next; 9 if(fast) 10 { 11 fast = fast->next; 12 slow = slow->next; 13 } 14 } 15 return slow; 16 }
快慢指針的其餘應用:指針
判斷單鏈表是否爲循環鏈表。code