Last active
February 28, 2021 15:00
-
-
Save mumunuu/9ac9bbe23ac92a52f7175d92f412d09d to your computer and use it in GitHub Desktop.
algorithm(leetcode) Reverse Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func reverseList(head *ListNode) *ListNode { | |
head = &ListNode{Next:head} | |
cur, curN := head, head | |
tmpArr := make([]int, 0); | |
for { | |
cur = cur.Next | |
if cur == nil { | |
break; | |
} | |
tmpArr = append(tmpArr, cur.Val) //cur로는 값을 전부다 담는다. | |
} | |
lenArr := len(tmpArr) - 1; | |
for i:=0; i<=lenArr; i++ { | |
curN = curN.Next | |
curN.Val = tmpArr[lenArr-i] //curN으로는 거꾸로 값을 넣어준다. | |
} | |
return head.Next //한번도 돌지 않은 head.Next로 반환. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment