反转链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
迭代
新建链表
两个指针,一个指向旧链表,一个指向新链表,不断从旧链表中复制节点,加到新链表头部。
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode * curr = nullptr;
while(head!=nullptr){
auto p = new ListNode(head->val);
p->next = curr;
curr = p;
head = head->next;
}
return curr;
}
};
原地
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode * curr = nullptr;
while(head!=nullptr){
// 记录旧链表当前节点
auto p = head;
// 旧链表指针后移
head = head->next;
// 将当前节点插入到新链表的头部
p->next = curr;
// 更新新链表的头部
curr = p;
}
return curr;
}
};