Skip to content

Commit

Permalink
feat: add solution to lc problem: No.0024 (#4045)
Browse files Browse the repository at this point in the history
  • Loading branch information
djvelimir authored Feb 7, 2025
1 parent 3ac8548 commit f92488f
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
60 changes: 60 additions & 0 deletions solution/0000-0099/0024.Swap Nodes in Pairs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions solution/0000-0099/0024.Swap Nodes in Pairs/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions solution/0000-0099/0024.Swap Nodes in Pairs/Solution.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 27 additions & 0 deletions solution/0000-0099/0024.Swap Nodes in Pairs/Solution2.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit f92488f

Please sign in to comment.