728x90
요즘 재귀에 꽂혀있음.
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(!head) { return head; }
ListNode* first= head;
ListNode* second = head->next;
ListNode* dummy = new ListNode(-1);
dummy->next = first;
ListNode* prev = dummy;
while(second){
ListNode* temp = second->next;
first->next = second->next;
second->next = first;
prev->next = second;
prev = first;
if(temp){
first = temp;
second = temp ->next;
}
else break;
}
return dummy->next;
}
};
728x90
'Archive' 카테고리의 다른 글
2D Platformer (0) | 2020.03.21 |
---|---|
3D Unity Portfolio (0) | 2020.03.21 |
Recursion (재귀) (0) | 2020.03.21 |
Chrono 로 퍼포먼스 체크하기 (0) | 2020.03.15 |
LeetCode - Two Sum (0) | 2020.03.13 |