Skip to content

P.230 Q17 풀이 질문드립니다. #141

Description

@Taemin90

제가 푼 부분은 아래와 같습니다.
즉 임시 변수(객체)인 temp를 사용하여 풀었습니다.
temp = cur.next
cur.next = cur.next.next
temp.next = cur

def algorithm(head):
    prev = ListNode(0, head)
    cur = head
    if head and head.next:
        head = head.next

    while cur and cur.next:
        prev.next = cur.next
        prev = cur
        temp = cur.next
        cur.next = cur.next.next
        temp.next = cur
        cur = cur.next
    return head

그런데 multiple assignment로 위에 두꺼운 부분을 아래와 같이 한 문장으로 줄이고자 하면 무한루프에 빠지게 됩니다.
cur.next, cur.next.next = cur.next.next, cur
제가 multiple assignment를 잘못 이해하고 있는건지.. 도저히 모르겠습니다.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions