일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 10926번
- v-on
- MSA
- 3003번
- Python
- LeetCode
- 코어자바스크립트
- 배열파티션
- 이벤트캡쳐링
- v-model
- 10869번
- JavaScript
- 프리코스
- v-for
- 백준
- 이벤트버블링
- 빅오표기법
- 2588번
- 도커
- vue
- v-if
- 객체지향의 사실과 오해
- 젠킨스
- DevOps
- hoisting
- 쿠버네티스
- 파이썬
- 리스트복사
- 실행 컨텍스트
- 우테코
Archives
- Today
- Total
새오의 개발 기록
Leetcode: 24 페어의 노드 스왑 본문
연결 리스트를 입력받아 페어 단위로 스왑하라
# 입력 예제
1->2->3->4
# 출력 예제
2->1->4->3
풀이
1. 반복
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0, head)
print(dummy)
prev, curr = dummy, head
while curr and curr.next:
next_pair = curr.next.next
second = curr.next
second.next = curr
curr.next = next_pair
prev.next = second
prev = curr
curr = next_pair
return dummy.next
'Algorithm > Leetcode' 카테고리의 다른 글
Leetcode 238: 자신을 제외한 배열의 곱 (0) | 2022.10.07 |
---|---|
Leetcode 561: 배열 파티션 1 (0) | 2022.10.06 |
Leetcode 1: 두 수의 합 (0) | 2022.10.03 |
Leetcode 49: 그룹 애너그램 (0) | 2022.10.02 |
Leetcode 819: 가장 흔한 단어 (0) | 2022.10.01 |