Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 95f200b

Browse files
Merge pull request youngyangyang04#1910 from 312885991/master
添加 0206.翻转链表 单纯使用虚拟头结点实现链表翻转
2 parents f12049b + e3f4f3c commit 95f200b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎problems/0206.翻转链表.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,33 @@ public class LinkNumbers
682682

683683

684684

685+
## 使用虚拟头结点解决链表翻转
686+
687+
> 使用虚拟头结点,通过头插法实现链表的翻转(不需要栈)
688+
689+
```java
690+
// 迭代方法:增加虚头结点,使用头插法实现链表翻转
691+
public static ListNode reverseList1(ListNode head) {
692+
// 创建虚头结点
693+
ListNode dumpyHead = new ListNode(-1);
694+
dumpyHead.next = null;
695+
// 遍历所有节点
696+
ListNode cur = head;
697+
while(cur != null){
698+
ListNode temp = cur.next;
699+
// 头插法
700+
cur.next = dumpyHead.next;
701+
dumpyHead.next = cur;
702+
cur = temp;
703+
}
704+
return dumpyHead.next;
705+
}
706+
```
707+
708+
709+
685710
## 使用栈解决反转链表的问题
711+
686712
* 首先将所有的结点入栈
687713
* 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。
688714

@@ -720,3 +746,4 @@ public ListNode reverseList(ListNode head) {
720746
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
721747
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
722748
</a>
749+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /