문제링크
https://leetcode.com/problems/rotate-list/description/?envType=study-plan-v2&envId=top-interview-150
📢 문제 간략설명
head 가 주어지면, k 값 만큼 오른쪽으로 회전한다.
📌풀이 알고리즘
📌소스코드 및 풀이방법
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if head == None:
return None
length = 1
tail = head
#노드의 길이 구하기
while tail.next:
tail = tail.next
length +=1
k = k % length
if k == 0:
return head
cur = head
for i in range(length -k -1):
cur = cur.next
newHead = cur.next #설정한 위치 저장
cur.next = None #배열의 끝으로 지정
tail.next = head #마지막 원소를 앞으로 땡김
return newHead
문제후기
그려보니까 이해함.
아우 머리야
참고