【面试题】剑指offer16--反转链表

来源:互联网 发布:c语言指令命令大全 编辑:程序博客网 时间:2024/05/24 04:19

反转链表,因为是单向链表,所以要调整指针指向的方向

还有在逆向的时候,反转后容易找不到原先的指向,所以要定义一个指针把原先的保存下来

代码实现:

#include<stdio.h>#include<stdlib.h>struct ListNode{int _key;ListNode* _next;};ListNode* ReverseList(ListNode* pHead){ListNode* pReversedHead = NULL;ListNode* pNode = pHead;ListNode* pPrev = NULL;while (pNode != NULL){ListNode* pNext = pNode->_next;if (pNext == NULL){pReversedHead = pHead;}pNext->_next = pPrev;pPrev = pNode;pNode = pNext;}return pReversedHead;}


原创粉丝点击