题目地址: https://leetcode-cn.com/problems/reverse-linked-list/
题目描述: 给你单链表的头节点 head ,反转链表,并
参考代码:
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *pre = nullptr;
ListNode *current = head;
while (current) { // 为什么不是 current->next ,如果是 最后一个节点就无法获取
ListNode *tmp = current->next;
current->next = pre;
pre = current;
current = tmp;
}
return pre; // 为什么不是 current current 是 nil
}
};
网友评论