码迷,mamicode.com
首页 > 其他好文 > 详细

单向链表翻转

时间:2020-04-02 22:45:25      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:bre   while   单向链表   list   use   printf   ==   str   翻转   

#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode 
{
	int val;
	struct ListNode *next;
}ListNode;

ListNode* ReverseList(ListNode* pHead)
{
	if (pHead == NULL)
		return pHead;
	ListNode* pre = NULL;
	ListNode* cur = pHead;
	ListNode* nxt = NULL;
	ListNode* res = NULL;
	while (cur != NULL)
	{
		nxt = cur->next;
		cur->next = pre;
		if (nxt == NULL)
			break;

		pre = cur;
		cur = nxt;
	}
	return cur;
}

int main()
{
	int i = 0;
	ListNode *pHead = NULL;
	while (i <= 5)
	{
		ListNode *pNew = (ListNode *)malloc(sizeof(ListNode));
		pNew->val = i++;
		pNew->next = pHead;
		pHead = pNew;
	}
	pHead = ReverseList(pHead);

	while (pHead)
	{
		printf("%d ", pHead->val);
		pHead = pHead->next;
	}
	system("pause");
	return 0;
}

  

单向链表翻转

标签:bre   while   单向链表   list   use   printf   ==   str   翻转   

原文地址:https://www.cnblogs.com/veis/p/12623112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!