-
[LeetCode] 206. Reverse Linked List 문제 풀이Algorithm/문제풀이 2021. 9. 8. 16:04
https://leetcode.com/problems/reverse-linked-list/
Reverse Linked List - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제 내용
링크드 리스트를 거꾸로 출력하면 된다.
문제 설명 문제 풀이
3개의 변수를 두고 노드 방향을 뒤집고 한칸씩 땡기고 했다.
class Solution { public: ListNode* reverseList(ListNode* head) { if (head == nullptr) return head; auto next1 = head->next; head->next = nullptr; while(head != nullptr && next1 != nullptr) { auto next2 = next1->next; next1->next = head; head = next1; next1 = next2; } return head; } };
'Algorithm > 문제풀이' 카테고리의 다른 글
[LeetCode] 21. Merge Two Sorted Lists 문제풀이 (0) 2021.09.08 [LeetCode] 848. Shifting Letters 문제풀이 (0) 2021.09.08 [LeetCode] 994. Rotting Oranges 문제 풀이 (0) 2021.09.08 [LeetCode] 542. 01 Matrix 문제 풀이 (0) 2021.09.08 [LeetCode] 116. Populating Next Right Pointers in Each Node 문제풀이 (0) 2021.09.06