Skip to content

Instantly share code, notes, and snippets.

@mumunuu
Last active February 28, 2021 15:00
Show Gist options
  • Save mumunuu/9ac9bbe23ac92a52f7175d92f412d09d to your computer and use it in GitHub Desktop.
Save mumunuu/9ac9bbe23ac92a52f7175d92f412d09d to your computer and use it in GitHub Desktop.
algorithm(leetcode) Reverse Linked List
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