Last active
February 28, 2021 16:46
-
-
Save mumunuu/f4e392cce76dc451c1073659fe13a1a4 to your computer and use it in GitHub Desktop.
algorithm(leetcode) Merge Two Sorted Lists
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 mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { | |
if l1 == nil { | |
return l2 | |
} | |
if l2 == nil { | |
return l1 | |
} | |
//구조체를 담아도 정렬할 수 있어서 해봄... | |
tmpArr := make([]ListNode, 0) | |
for l1 != nil { | |
val := ListNode{ | |
Val: l1.Val, | |
} | |
tmpArr = append(tmpArr, val) | |
l1 = l1.Next | |
} | |
for l2 != nil { | |
val := ListNode{ | |
Val: l2.Val, | |
} | |
tmpArr = append(tmpArr, val) | |
l2 = l2.Next | |
} | |
//NodeList를 다 담고나서, 정렬 | |
sort.Slice(tmpArr, func(i, j int) bool { | |
return tmpArr[i].Val < tmpArr[j].Val | |
}) | |
var head ListNode; | |
answer, answerN := &head, &head | |
//배열의 값과, Next값은 다음 값의 & 연산자 값을 넣어주면 됨 | |
for i:=0; i+1<len(tmpArr); i++ { | |
answerN.Val = tmpArr[i].Val | |
answerN.Next = &tmpArr[i+1] | |
answerN = answerN.Next | |
} | |
return answer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment