전체 글 177

<리트코드> 21번 파이썬 알고리즘 [연결 리스트]

def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: dummy = ListNode(0, head) prev, cur = dummy, head while cur and cur.next: # save ptrs nxtPair = cur.next.next scd = cur.next # reverse cur.next = nxtPair scd.next = cur prev.next = scd # update prev = cur cur = nxtPair return dummy.next # 필요한 것 처음에 무엇을 저장할 포인터로 설정 해야 할지 생각하기 이후에 reverse 코드 짜기 다음으로 업데이트 하기

알고리즘 2021.12.01