2020-06-22
1.题目描述
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
2.解答
直接进行反转即可,注意不要让链表断了即可
3.代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head) return NULL;
ListNode* p = head,*q=p->next,*t;
p->next=NULL;
while (q){
t=q->next;
q->next=p;
p=q;
q=t;
}
return p;
}
};