-
[LeetCode] 206. Reverse Linked List c++Coding Test/LeetCode 2023. 1. 4. 23:00728x90
문제
Given the head of a singly linked list, reverse the list, and return the reversed list.
단일 링크드 리스트가 주어지면 순서를 뒤집는 문제입니다.
문제 해결
class Solution { public: ListNode* reverseList(ListNode* head) { ListNode *nextNode, *prevNode = NULL; while(head){ nextNode = head->next; head->next = prevNode; prevNode = head; head = nextNode; } return prevNode; } };
다른 풀이
class Solution { public: ListNode* reverseList(ListNode* head, ListNode* nextNode = NULL, ListNode* prevNode = NULL) { return head ? reverseList(head->next, (head->next = prevNode, nextNode), head) : prevNode; } };
728x90'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 653. Two Sum IV - Input is a BST c++ (0) 2023.02.22 [LeetCode] 1464. Maximum Product of Two Elements in an Array c++ (0) 2023.02.21 [LeetCode] 21. Merge Two Sorted Lists c++ (0) 2023.01.04 [LeetCode] 994. Rotting Oranges c++ (0) 2023.01.04 [LeetCode] 542. 01 Matrix c++ (0) 2023.01.03