From 87a6e78e8106d769576ddc48770deb245f830fd0 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:15:40 +0800 Subject: [PATCH] =?UTF-8?q?Update=200024.=E4=BA=A4=E6=8D=A2=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E8=8A=82=E7=82=B9=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0024.两两交换链表中的节点.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 36f9b0cc..57034f47 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -459,6 +459,40 @@ impl Solution { } ``` +### C# +```C# +// 虚拟头结点 +public ListNode SwapPairs(ListNode head) +{ + var dummyHead = new ListNode(); + dummyHead.next = head; + ListNode cur = dummyHead; + while (cur.next != null && cur.next.next != null) + { + ListNode tmp1 = cur.next; + ListNode tmp2 = cur.next.next.next; + + cur.next = cur.next.next; + cur.next.next = tmp1; + cur.next.next.next = tmp2; + + cur = cur.next.next; + } + return dummyHead.next; +} +``` +``` C# +// 递归 +public ListNode SwapPairs(ListNode head) +{ + if (head == null || head.next == null) return head; + var cur = head.next; + head.next = SwapPairs(head.next.next); + cur.next = head; + return cur; +} +``` +