c++链表获取长度,链表翻转, 查找链表倒数第K个节点以及中间点
测试数据
ListNode* pHead = new ListNode();
pHead->m_key = 1;
ListNode* pNode = pHead;
for (int i = 2; i <= 5; i++)
{
ListNode* pNew = new ListNode();
pNew->m_key = i;
pNew->pNext = nullptr;
pNode->pNext = pNew;
pNode = pNew;
}
pHead = reverseList(pHead);
for (ListNode* pNode = pHead; pNode != nullptr; pNode = pNode->pNext)
{
cout << pNode->m_key << endl;
}
1:获得链表的长度
struct ListNode
{
int m_key;//数据域
ListNode* pNext;