反转链表

来源:互联网 发布:网络实体小说排行榜 编辑:程序博客网 时间:2024/05/17 20:29
//定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点。#include "stdlib.h"struct ListNode{int m_nKey;ListNode* m_pNext;};ListNode* ReverseList(ListNode* pHead){if (pHead == NULL || pHead->m_pNext == NULL)return pHead;ListNode* current = pHead->m_pNext;ListNode* currentAhead = pHead;ListNode* currentBehind = current->m_pNext;current->m_pNext = currentAhead;currentAhead->m_pNext = NULL;while (currentBehind != NULL){currentAhead = current;current = currentBehind;currentBehind = current->m_pNext;current->m_pNext = currentAhead;}return current;}


0 0