问题概述:
快速找到未知长度单链表的中间结点。
代码实现:
1 Status GetMidNode(LinkList L, ElemType *e) 2 { 3 LinkList search, mid; 4 mid = search = L; 5 6 while (search->next != NULL) 7 { 8 //search移动的速度是 mid 的2倍 9 if (search->next->next != NULL) 10 { 11 search = search->next->next; 12 mid = mid->next; 13 } 14 else 15 { 16 search = search->next; 17 } 18 } 19 20 *e = mid->data; 21 22 return OK; 23 }
时间: 2024-11-06 16:40:09