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

203 #23

Open
happysmile12321 opened this issue Mar 17, 2024 · 1 comment
Open

203 #23

happysmile12321 opened this issue Mar 17, 2024 · 1 comment

Comments

@happysmile12321
Copy link
Owner

感悟:解一道题最简单直观的感悟。
一道题,我解出来可能非常费劲。
但是多加些辅助变量,尽可能把循环写的简单点,就会少出很多错。

@happysmile12321
Copy link
Owner Author

很自然的一点,我会想到:
1->2->3,如果开始遍历的话,以2为例,将2移除,则需要2前面的那个指针指向2后面的那个指针。
而这是单向链表,指针数好像有点不够,所以需要将遍历时遍历2前面那一位。

然而,这样做,毫无疑问会增加思考的复杂度,而增加思考的复杂度,就会增加出错的风险。
我们为什么不能以牺牲一些辅助变量的代价而使算法更安全呢?

所以,最简单直接的做法其实是直接判断2这个节点,他没有pre,我就给他pre。
需要注意循环判断条件,if和else。

接着,需要引入一个虚拟头结点的概念。

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

No branches or pull requests

1 participant