Skip to content

[bus710] Week 07 #928

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

Merged
merged 1 commit into from
Jan 24, 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
26 changes: 26 additions & 0 deletions reverse-linked-list/bus710.go
Copy link
Contributor

Choose a reason for hiding this comment

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

반복문 내에서 계속 새로운 ListNode struct instance를 생성하는 것은 공간 복잡도 측면에서 효율적이지 않습니다
포인터만을 이용하는 것을 추천드립니다 :)
제가 제안하는 솔루션 또한 첨부합니다, 정 생각이 안 나시거나 바쁘실 때 참고해주세요 (https://gist.github.com/obzva/c5f726474e844e076270aad523acc39e)

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package hello
Copy link
Contributor

Choose a reason for hiding this comment

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

안녕하세요 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

문제 풀이를 할 때 테스트를 먼저 작성하는 편인데, 패키지 이름을 짧게 적다보니 이렇게 되었습니다 ㅎㅎ


type ListNode struct {
Val int
Next *ListNode
}

func reverseList(head *ListNode) *ListNode {
head2 := &ListNode{}

if head == nil {
return nil
}
Comment on lines +9 to +13
Copy link
Contributor

Choose a reason for hiding this comment

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

숏컷 로직이 제일 상단에 위치하는게 좋을 것 같아요

Suggested change
head2 := &ListNode{}
if head == nil {
return nil
}
if head == nil {
return nil
}
head2 := &ListNode{}

Copy link
Contributor

Choose a reason for hiding this comment

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

그리고 head2보다 더 나은 이름이 있을 것 같아요
이 변수가 뭐하는 녀석인지 바로 와닿지가 않았습니다

Copy link
Contributor Author

Choose a reason for hiding this comment

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

그렇네요, 할당 하기 전에 반환하는게 더 나을 것 같습니다.
반환하는 변수 이름도 신경 쓰겠습니다.


for {
head2.Val = head.Val
if head.Next == nil {
break
}
head = head.Next
temp := &ListNode{Next: head2}
head2 = temp
}

return head2
}
Loading