#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;
}

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
  • 2021-10-14
  • 2021-07-29
猜你喜欢
  • 2021-12-26
  • 2021-12-02
  • 2022-12-23
  • 2021-11-10
  • 2021-07-09
  • 2022-03-08
  • 2021-10-23
相关资源
相似解决方案