From 10cccc211edf7c96bb7d5c0fe580b0469ff8ed55 Mon Sep 17 00:00:00 2001 From: huhaonan <812559558@qq.com> Date: Thu, 8 Dec 2022 10:16:04 +0800 Subject: [PATCH] =?UTF-8?q?0024Java=E7=89=88=E6=8C=89=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E6=80=9D=E8=B7=AF=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0024.两两交换链表中的节点.md | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index de389448..4dc051aa 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -155,22 +155,24 @@ class Solution { ```java class Solution { - ListNode dumyhead = new ListNode(-1); - dumyhead.next = head; // 设置一个虚拟头结点 - ListNode cur1 = dumyhead; // 将虚拟头结点指向head,这样方面后面做删除操作 + public ListNode swapPairs(ListNode head) { + ListNode dumyhead = new ListNode(-1); // 设置一个虚拟头结点 + dumyhead.next = head; // 将虚拟头结点指向head,这样方面后面做删除操作 + ListNode cur = dumyhead; ListNode temp; // 临时节点,保存两个节点后面的节点 ListNode firstnode; // 临时节点,保存两个节点之中的第一个节点 ListNode secondnode; // 临时节点,保存两个节点之中的第二个节点 - while (cur1.next != null && cur1.next.next != null) { - temp = cur1.next.next.next; - firstnode = cur1.next; - secondnode = cur1.next.next; - cur1.next = secondnode; // 步骤一 + while (cur.next != null && cur.next.next != null) { + temp = cur.next.next.next; + firstnode = cur.next; + secondnode = cur.next.next; + cur.next = secondnode; // 步骤一 secondnode.next = firstnode; // 步骤二 firstnode.next = temp; // 步骤三 - cur1 = firstnode; // cur移动,准备下一轮交换 + cur = firstnode; // cur移动,准备下一轮交换 } - return dumyhead.next; + return dumyhead.next; + } } ``` @@ -429,4 +431,3 @@ impl Solution { -