Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bus710] week 04 #834

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions merge-two-sorted-lists/bus710.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package hello

type ListNode struct {
Val int
Next *ListNode
}

func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
if list1 == nil && list2 == nil {
return nil
} else if list1 == nil {
return list2
} else if list2 == nil {
return list1
}

newList := &ListNode{}
cur := newList

for {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1-loop으로 잘 풀어내셨군요! 다음 제출부터 혹시 주석으로 time/space complexity를 적어주시면, 리뷰할때 도움이 될 것 같습니다 :)

  • 개인적인 궁금점 하나만 질문하자면 for vs while 중 이 문제에서 for을 사용하신 이유가 있으신가요? (개인적 취향에 가까워서 그냥 궁금합니다 🤣

switch {
case list1.Val < list2.Val:
cur.Next = &ListNode{Val: list1.Val}
list1 = list1.Next
case list1.Val > list2.Val:
cur.Next = &ListNode{Val: list2.Val}
list2 = list2.Next
default:
cur.Next = &ListNode{Val: list1.Val}
list1 = list1.Next
cur = cur.Next
cur.Next = &ListNode{Val: list2.Val}
list2 = list2.Next
}
cur = cur.Next
if list1 == nil && list2 == nil && cur.Next == nil {
break
}

if list1 == nil {
cur.Next = list2
break
}
if list2 == nil {
cur.Next = list1
break
}

}

return newList.Next
}
Loading