/**
* 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 *prev = nullptr;
ListNode *curr = head;
while(head != nullptr) {
curr = curr->next;
head->next = prev;
prev = head;
head = curr;
}
return prev;
}
};
Data Structure 수업을 듣고 링크드 리스트를 제대로 이해했다면 너무나도 쉽게 넘어갈 수 있는 문제였습니다.
문제: https://leetcode.com/problems/reverse-linked-list/
'Problem Solving > 리트코드(leetcode)' 카테고리의 다른 글
[Leetcode/C++] 168. Excel Sheet Column Title (0) | 2022.01.24 |
---|---|
[Leetcode/C++] 682. Baseball Game (0) | 2022.01.24 |
[Leetcode/C++] 496. Next Greater Element 1 (0) | 2022.01.24 |
[Leetcode/C++] 1021. Remove Outermost Parentheses (0) | 2022.01.24 |
[Leetcode/C++] 551. Student Attendance Record I (0) | 2022.01.24 |