LeetCode 206.Reverse Linked List

단지·2024년 1월 8일
0

Python

목록 보기
2/3

Given the head of a singly linked list, reverse the list, and return the reversed list.

링크드리스트를 뒤집어서 리턴해라

전체코드

한줄씩

Input

cur = head

head = head.next

cur.next = pre
(장외)

pre = cur

한바퀴 완
두바퀴
cur = head

head = head.next

cur.next = pre

pre = cur

세바퀴
cur = head

head = head.next

cur.next = pre

pre = cur

이런 느낌으로 쭉 돌리고 마지막 노드를 반환하면
[5, 4, 3, 2, 1]로 출력 된다

어려웠는데 그림으로 그려서 생각해보니 이해가 잘 되었다

profile
'-'b

0개의 댓글