diff --git a/solution/0000-0099/0024.Swap Nodes in Pairs/README.md b/solution/0000-0099/0024.Swap Nodes in Pairs/README.md index 38a1b4904219d..e2b5f98ec92b8 100644 --- a/solution/0000-0099/0024.Swap Nodes in Pairs/README.md +++ b/solution/0000-0099/0024.Swap Nodes in Pairs/README.md @@ -256,6 +256,34 @@ var swapPairs = function (head) { }; ``` +#### C# + +```cs +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int val=0, ListNode next=null) { + * this.val = val; + * this.next = next; + * } + * } + */ +public class Solution { + public ListNode SwapPairs(ListNode head) { + if (head is null || head.next is null) { + return head; + } + ListNode t = SwapPairs(head.next.next); + ListNode p = head.next; + p.next = head; + head.next = t; + return p; + } +} +``` + #### Ruby ```rb @@ -466,6 +494,38 @@ var swapPairs = function (head) { }; ``` +#### C# + +```cs +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int val=0, ListNode next=null) { + * this.val = val; + * this.next = next; + * } + * } + */ +public class Solution { + public ListNode SwapPairs(ListNode head) { + ListNode dummy = new ListNode(0, head); + ListNode pre = dummy; + ListNode cur = head; + while (cur is not null && cur.next is not null) { + ListNode t = cur.next; + cur.next = t.next; + t.next = cur; + pre.next = t; + pre = cur; + cur = cur.next; + } + return dummy.next; + } +} +``` + #### PHP ```php diff --git a/solution/0000-0099/0024.Swap Nodes in Pairs/README_EN.md b/solution/0000-0099/0024.Swap Nodes in Pairs/README_EN.md index f2a3dc01f612a..e9340a634752c 100644 --- a/solution/0000-0099/0024.Swap Nodes in Pairs/README_EN.md +++ b/solution/0000-0099/0024.Swap Nodes in Pairs/README_EN.md @@ -269,6 +269,34 @@ var swapPairs = function (head) { }; ``` +#### C# + +```cs +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int val=0, ListNode next=null) { + * this.val = val; + * this.next = next; + * } + * } + */ +public class Solution { + public ListNode SwapPairs(ListNode head) { + if (head is null || head.next is null) { + return head; + } + ListNode t = SwapPairs(head.next.next); + ListNode p = head.next; + p.next = head; + head.next = t; + return p; + } +} +``` + #### Ruby ```rb @@ -479,6 +507,38 @@ var swapPairs = function (head) { }; ``` +#### C# + +```cs +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int val=0, ListNode next=null) { + * this.val = val; + * this.next = next; + * } + * } + */ +public class Solution { + public ListNode SwapPairs(ListNode head) { + ListNode dummy = new ListNode(0, head); + ListNode pre = dummy; + ListNode cur = head; + while (cur is not null && cur.next is not null) { + ListNode t = cur.next; + cur.next = t.next; + t.next = cur; + pre.next = t; + pre = cur; + cur = cur.next; + } + return dummy.next; + } +} +``` + #### PHP ```php diff --git a/solution/0000-0099/0024.Swap Nodes in Pairs/Solution.cs b/solution/0000-0099/0024.Swap Nodes in Pairs/Solution.cs new file mode 100644 index 0000000000000..ddeda2166b005 --- /dev/null +++ b/solution/0000-0099/0024.Swap Nodes in Pairs/Solution.cs @@ -0,0 +1,23 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int val=0, ListNode next=null) { + * this.val = val; + * this.next = next; + * } + * } + */ +public class Solution { + public ListNode SwapPairs(ListNode head) { + if (head is null || head.next is null) { + return head; + } + ListNode t = SwapPairs(head.next.next); + ListNode p = head.next; + p.next = head; + head.next = t; + return p; + } +} \ No newline at end of file diff --git a/solution/0000-0099/0024.Swap Nodes in Pairs/Solution2.cs b/solution/0000-0099/0024.Swap Nodes in Pairs/Solution2.cs new file mode 100644 index 0000000000000..aee6f3c492711 --- /dev/null +++ b/solution/0000-0099/0024.Swap Nodes in Pairs/Solution2.cs @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int val=0, ListNode next=null) { + * this.val = val; + * this.next = next; + * } + * } + */ +public class Solution { + public ListNode SwapPairs(ListNode head) { + ListNode dummy = new ListNode(0, head); + ListNode pre = dummy; + ListNode cur = head; + while (cur is not null && cur.next is not null) { + ListNode t = cur.next; + cur.next = t.next; + t.next = cur; + pre.next = t; + pre = cur; + cur = cur.next; + } + return dummy.next; + } +} \ No newline at end of file