单链表 --- 查找链表中间节点,仅一次遍历
方法:快慢指针法
#include<string.h>
#include<malloc.h>
#include<assert.h>
typedef int DataType;
typedef struct ListNode
{
struct ListNode *_next;
DataType _data;
}ListNode;
void PrintList(ListNode *&pHead)
{
while(pHead)
{
printf("%d->",pHead->_data);
pHead=pHead->_next;
}
printf("NULL\n");
}
void FindMid(ListNode *&pHead) //查找链表中间节点,仅一次遍历
{
ListNode *slow=pHead;
ListNode *fast=pHead;
while (fast&&fast->_next&&fast->_next->_next)
{
fast = fast->_next->_next;
slow = slow->_next;
}
printf("链表中间节点:%d\n",slow->_data);
}
转载于:https://blog.51cto.com/zxtong/1757672