LeetCode 0206 - Reverse Linked List
# Hints
水题
链表
# 题面
Difficulty | Time Complexity Limit | Extra-Memory Complexity Limit |
---|---|---|
Easy |
Given the head
of a singly linked list, reverse the list, and return the reversed list.
Example 1:
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Example 2:
Input: head = [1,2]
Output: [2,1]
Example 3:
Input: head = []
Output: []
# 题意
给定一个单链表,要求将链表顺序翻转。
# 题解
水题,遍历一遍即可。
# AC代码
/**
* 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 * prv = nullptr;
ListNode * cur = head;
while (cur != nullptr) {
ListNode * nxt = cur->next;
cur->next = prv;
prv = cur;
cur = nxt;
}
// 返回
return prv;
}
};
- 01
- Reading Papers - Kernel Concurrency06-01
- 02
- Linux Kernel - Source Code Overview05-01
- 03
- Linux Kernel - Per-CPU Storage05-01