File tree Expand file tree Collapse file tree 1 file changed +99
-0
lines changed Expand file tree Collapse file tree 1 file changed +99
-0
lines changed Original file line number Diff line number Diff line change 1+ ![ ] ( https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png )
2+ > 仰望星空的人,不应该被嘲笑
3+
4+ ## 题目描述
5+
6+ 反转一个单链表。
7+
8+ 示例:
9+
10+ ``` javascript
11+ 输入: 1 - > 2 - > 3 - > 4 - > 5 - > NULL
12+ 输出: 5 - > 4 - > 3 - > 2 - > 1 - > NULL
13+ ```
14+
15+ 进阶:
16+ 你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
17+
18+ 来源:力扣(LeetCode)
19+ 链接:https://leetcode-cn.com/problems/reverse-linked-list
20+ 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
21+
22+
23+
24+ ## 解题思路
25+
26+ ** 非递归解法**
27+
28+ ``` javascript
29+ /**
30+ * Definition for singly-linked list.
31+ * function ListNode(val) {
32+ * this.val = val;
33+ * this.next = null;
34+ * }
35+ */
36+ /**
37+ * @param {ListNode} head
38+ * @return {ListNode}
39+ */
40+ var reverseList = function (head ) {
41+ let pre = null ;
42+ let cur = head;
43+ while (cur){
44+ let tmp = cur .next ;
45+ cur .next = pre;
46+ pre = cur;
47+ cur = tmp;
48+ }
49+ return pre;
50+ };
51+ ```
52+
53+ ** 递归解法**
54+
55+ ``` javascript
56+ /**
57+ * Definition for singly-linked list.
58+ * function ListNode(val) {
59+ * this.val = val;
60+ * this.next = null;
61+ * }
62+ */
63+ /**
64+ * @param {ListNode} head
65+ * @return {ListNode}
66+ */
67+ var reverseList = function (head ) {
68+ let reverse = (pre , cur ) => {
69+ if (! cur) return pre;
70+ let tmp = cur .next ;
71+ cur .next = pre;
72+ return reverse (cur, tmp);
73+ }
74+ return reverse (null , head);
75+ };
76+ ```
77+
78+
79+ ## 最后
80+ 文章产出不易,还望各位小伙伴们支持一波!
81+
82+ 往期精选:
83+
84+ <a href =" https://github.com/Chocolate1999/Front-end-learning-to-organize-notes " >小狮子前端の笔记仓库</a >
85+
86+ <a href =" https://github.com/Chocolate1999/leetcode-javascript " >leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)</a >
87+
88+ 小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
89+
90+
91+ <a href =" https://yangchaoyi.vip/ " >访问超逸の博客</a >,方便小伙伴阅读玩耍~
92+
93+ ![ ] ( https://img-blog.csdnimg.cn/2020090211491121.png#pic_center )
94+
95+ ``` javascript
96+ 学如逆水行舟,不进则退
97+ ```
98+
99+
You can’t perform that action at this time.
0 commit comments