得到链表的中间节点
1 ListNode* getMid(ListNode* head){
2 ListNode* fast = head, * slow = head;
3 while(fast->next && fast->next->next){
4 slow = slow->next;
5 fast = fast->next->next;
6 }
7 // delete fast;不注釋掉就RA
8 return slow;
9 }
getMid函數中,fast一次走2步,slow一次走1步,因此假設全長len,len偶時,slow到達前半段的最后一個節點,len奇時,slow到達正中間的節點,兩種情況中,slow->next均為后半段的起始節點。
轉載于:https://www.cnblogs.com/co0oder/p/5350504.html
總結
- 上一篇: GIMP简介
- 下一篇: 【转】SSL/TLS/WTLS协议原理