得到链表的中间节点
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函數(shù)中,fast一次走2步,slow一次走1步,因此假設(shè)全長(zhǎng)len,len偶時(shí),slow到達(dá)前半段的最后一個(gè)節(jié)點(diǎn),len奇時(shí),slow到達(dá)正中間的節(jié)點(diǎn),兩種情況中,slow->next均為后半段的起始節(jié)點(diǎn)。
轉(zhuǎn)載于:https://www.cnblogs.com/co0oder/p/5350504.html
總結(jié)
- 上一篇: GIMP简介
- 下一篇: 【转】SSL/TLS/WTLS协议原理