单链表-反转

问题描述

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
示范输入
示范输入

解答代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/

struct ListNode* reverseList(struct ListNode* head){
struct ListNode *pre = NULL, *cur = head, *next = NULL;
while (cur) {
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
-------------本文结束 感谢阅读-------------