We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9ceecd5 commit 060f1eaCopy full SHA for 060f1ea
problems/0024.两两交换链表中的节点.md
@@ -86,7 +86,27 @@ public:
86
87
88
Java:
89
-
+```java
90
+// 虚拟头结点
91
+class Solution {
92
+ public ListNode swapPairs(ListNode head) {
93
+
94
+ ListNode dummyNode = new ListNode(0);
95
+ dummyNode.next = head;
96
+ ListNode prev = dummyNode;
97
98
+ while (prev.next != null && prev.next.next != null) {
99
+ ListNode temp = head.next.next; // 缓存 next
100
+ prev.next = head.next; // 将 prev 的 next 改为 head 的 next
101
+ head.next.next = head; // 将 head.next(prev.next) 的next,指向 head
102
+ head.next = temp; // 将head 的 next 接上缓存的temp
103
+ prev = head; // 步进1位
104
+ head = head.next; // 步进1位
105
+ }
106
+ return dummyNode.next;
107
108
+}
109
+```
110
111
Python:
112
0 commit comments