Skip to content

206. Reverse Linked List_new.md #10

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

lilnoahhh
Copy link
Owner

206. Reverse Linked List
Given the head of a singly linked list, reverse the list, and return the reversed list.
次は703. Kth Largest Element in a Stream

return reversed_head
```
これreversed_head=stack.pop()まではできていたので悔しい。
araiさんはこの問題をstackに分類していたので、これが想定解なのかな。
Copy link

Choose a reason for hiding this comment

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

current.next = None
return reversed_head
```
これreversed_head=stack.pop()まではできていたので悔しい。
Copy link

Choose a reason for hiding this comment

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

これ、stack の中で1つ目だけが特殊なのです。
なので、番兵を立てるか、分岐をするか、ループの外に出すかの選択になります。どれでもいいので発想できれば書けたでしょう。

あと、stack の中のノードがこの時点ではまだ .next で全部繋がっていることも意識しましょう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants