Skip to content

Commit

Permalink
feat: update solution to lc problem: No.0024
Browse files Browse the repository at this point in the history
No.0024.Swap Nodes in Pairs
  • Loading branch information
poltao committed Nov 20, 2022
1 parent 1a8fc8b commit 1efa4f8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
2 changes: 1 addition & 1 deletion solution/0000-0099/0018.4Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

**方法一:排序 + 双指针**

该题和 [0015.三数之和](../0015.3Sum/README.md) 相似,解法也相似。
该题和 [0015.三数之和](https://leetcode.cn/problems/3sum/) 相似,解法也相似。

时间复杂度为 $O(n^3)$,空间复杂度为 $O(\log n)$,其中 $n$ 是数组的长度。

Expand Down
31 changes: 31 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 @@ -44,8 +44,16 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:迭代**

设置虚拟头节点 dummy,pre 指针初始指向 dummy,遍历链表,每次交换 pre 后面的两个节点即可。

时间复杂度为 $O(n)$,空间复杂度为 $O(1)$,其中 $n$ 是链表的长度。

**方法二:递归**

时间复杂度为 $O(n)$,空间复杂度为 $O(n)$,其中 $n$ 是链表的长度。

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -166,6 +174,8 @@ public:
### **Go**
迭代:
```go
/**
* Definition for singly-linked list.
Expand All @@ -189,6 +199,27 @@ func swapPairs(head *ListNode) *ListNode {
}
```

递归:

```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func swapPairs(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
res := swapPairs(head.Next.Next)
p := head.Next
p.Next, head.Next = head, res
return p
}
```

### **Ruby**

```rb
Expand Down
31 changes: 31 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 @@ -38,6 +38,14 @@

## Solutions

**Approach 1: Iteration**

Time complexity $O(n)$, Space complexity $O(1)$.

**Approach 2: Recursion**

Time complexity $O(n)$, Space complexity $O(n)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -154,6 +162,8 @@ public:
### **Go**
Iteration:
```go
/**
* Definition for singly-linked list.
Expand All @@ -177,6 +187,27 @@ func swapPairs(head *ListNode) *ListNode {
}
```

Recursion:

```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func swapPairs(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
res := swapPairs(head.Next.Next)
p := head.Next
p.Next, head.Next = head, res
return p
}
```

### **Ruby**

```rb
Expand Down
20 changes: 8 additions & 12 deletions solution/0000-0099/0024.Swap Nodes in Pairs/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
* Next *ListNode
* }
*/
func swapPairs(head *ListNode) *ListNode {
dummy := &ListNode{0, head}
pre, cur := dummy, head
for cur != nil && cur.Next != nil {
t := cur.Next
cur.Next = t.Next
t.Next = cur
pre.Next = t
pre = cur
cur = cur.Next
}
return dummy.Next
func swapPairs(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
res := swapPairs(head.Next.Next)
p := head.Next
p.Next, head.Next = head, res
return p
}

0 comments on commit 1efa4f8

Please sign in to comment.