Skip to content

Commit 240714e

Browse files
committed
feat: update solutions to leetcode problem: No.0024
1 parent cb0c4f4 commit 240714e

File tree

6 files changed

+115
-43
lines changed

6 files changed

+115
-43
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<a href="https://opencollective.com/doocs-leetcode/backers/badge.svg" alt="backers on Open Collective"><img src="https://opencollective.com/doocs-leetcode/backers/badge.svg" /></a>
88
<a href="https://opencollective.com/doocs-leetcode/sponsors/badge.svg" alt="Sponsors on Open Collective"><img src="https://opencollective.com/doocs-leetcode/sponsors/badge.svg" /></a>
99
<a href="https://github.com/doocs/leetcode/blob/main/LICENSE"><img src="https://badgen.net/github/license/doocs/leetcode?color=green" alt="LICENSE"></a><br>
10-
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode?color=cyan" alt="stars"></a>
11-
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://badgen.net/github/forks/doocs/leetcode?color=cyan" alt="forks"></a>
10+
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode?color=cyan&cache=300" alt="stars"></a>
11+
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://badgen.net/github/forks/doocs/leetcode?color=cyan&cache=300" alt="forks"></a>
1212
<a href="https://github.com/doocs/leetcode"><img src="https://badgen.net/badge/⭐/GitHub/cyan" alt="github"></a>
1313
<a href="https://gitee.com/doocs/leetcode"><img src="https://badgen.net/badge/⭐/Gitee/cyan" alt="github"></a>
1414
<a href="http://makeapullrequest.com"><img src="https://badgen.net/badge/PRs/welcome/cyan" alt="PRs Welcome"></a>

solution/0000-0099/0024.Swap Nodes in Pairs/README.md

+43-10
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ class Solution:
3737
dummy = ListNode(next=head)
3838
pre, cur = dummy, head
3939
while cur and cur.next:
40-
pre.next = cur.next
41-
t = cur.next.next
42-
cur.next.next = cur
43-
cur.next = t
40+
t = cur.next
41+
cur.next = t.next
42+
t.next = cur
43+
pre.next = t
4444
pre = cur
45-
cur = cur.next
45+
cur = pre.next
4646
return dummy.next
4747
```
4848

@@ -66,18 +66,51 @@ class Solution {
6666
ListNode dummy = new ListNode(0, head);
6767
ListNode pre = dummy, cur = head;
6868
while (cur != null && cur.next != null) {
69-
pre.next = cur.next;
70-
ListNode t = cur.next.next;
71-
cur.next.next = cur;
72-
cur.next = t;
69+
ListNode t = cur.next;
70+
cur.next = t.next;
71+
t.next = cur;
72+
pre.next = t;
7373
pre = cur;
74-
cur = cur.next;
74+
cur = pre.next;
75+
7576
}
7677
return dummy.next;
7778
}
7879
}
7980
```
8081

82+
### **C++**
83+
84+
```cpp
85+
/**
86+
* Definition for singly-linked list.
87+
* struct ListNode {
88+
* int val;
89+
* ListNode *next;
90+
* ListNode() : val(0), next(nullptr) {}
91+
* ListNode(int x) : val(x), next(nullptr) {}
92+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
93+
* };
94+
*/
95+
class Solution {
96+
public:
97+
ListNode* swapPairs(ListNode* head) {
98+
ListNode* dummy = new ListNode(0, head);
99+
ListNode* pre = dummy;
100+
ListNode* cur = head;
101+
while (cur != nullptr && cur->next != nullptr) {
102+
ListNode* t = cur->next;
103+
cur->next = t->next;
104+
t->next = cur;
105+
pre->next = t;
106+
pre = cur;
107+
cur = pre->next;
108+
}
109+
return dummy->next;
110+
}
111+
};
112+
```
113+
81114
### **...**
82115
83116
```

solution/0000-0099/0024.Swap Nodes in Pairs/README_EN.md

+43-10
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ class Solution:
3535
dummy = ListNode(next=head)
3636
pre, cur = dummy, head
3737
while cur and cur.next:
38-
pre.next = cur.next
39-
t = cur.next.next
40-
cur.next.next = cur
41-
cur.next = t
38+
t = cur.next
39+
cur.next = t.next
40+
t.next = cur
41+
pre.next = t
4242
pre = cur
43-
cur = cur.next
43+
cur = pre.next
4444
return dummy.next
4545
```
4646

@@ -62,18 +62,51 @@ class Solution {
6262
ListNode dummy = new ListNode(0, head);
6363
ListNode pre = dummy, cur = head;
6464
while (cur != null && cur.next != null) {
65-
pre.next = cur.next;
66-
ListNode t = cur.next.next;
67-
cur.next.next = cur;
68-
cur.next = t;
65+
ListNode t = cur.next;
66+
cur.next = t.next;
67+
t.next = cur;
68+
pre.next = t;
6969
pre = cur;
70-
cur = cur.next;
70+
cur = pre.next;
71+
7172
}
7273
return dummy.next;
7374
}
7475
}
7576
```
7677

78+
### **C++**
79+
80+
```cpp
81+
/**
82+
* Definition for singly-linked list.
83+
* struct ListNode {
84+
* int val;
85+
* ListNode *next;
86+
* ListNode() : val(0), next(nullptr) {}
87+
* ListNode(int x) : val(x), next(nullptr) {}
88+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
89+
* };
90+
*/
91+
class Solution {
92+
public:
93+
ListNode* swapPairs(ListNode* head) {
94+
ListNode* dummy = new ListNode(0, head);
95+
ListNode* pre = dummy;
96+
ListNode* cur = head;
97+
while (cur != nullptr && cur->next != nullptr) {
98+
ListNode* t = cur->next;
99+
cur->next = t->next;
100+
t->next = cur;
101+
pre->next = t;
102+
pre = cur;
103+
cur = pre->next;
104+
}
105+
return dummy->next;
106+
}
107+
};
108+
```
109+
77110
### **...**
78111
79112
```

solution/0000-0099/0024.Swap Nodes in Pairs/Solution.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
* struct ListNode {
44
* int val;
55
* ListNode *next;
6-
* ListNode(int x) : val(x), next(NULL) {}
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
79
* };
810
*/
911
class Solution {
1012
public:
1113
ListNode* swapPairs(ListNode* head) {
12-
if(head==NULL||head->next==NULL)//递归的最小情况,链表为空,或者长度为1,直接返回
13-
return head;
14-
ListNode *temp=head->next;//定义temp为head->next的node
15-
ListNode *nextChain=swapPairs(head->next->next);//递归调用得到head->next->next链表交换结果
16-
head->next=nextChain;//head的next指向递归后的结果
17-
temp->next=head;//temp也就是之前的head->next节点指向head,那么现在的head为temp
18-
return temp;
14+
ListNode* dummy = new ListNode(0, head);
15+
ListNode* pre = dummy;
16+
ListNode* cur = head;
17+
while (cur != nullptr && cur->next != nullptr) {
18+
ListNode* t = cur->next;
19+
cur->next = t->next;
20+
t->next = cur;
21+
pre->next = t;
22+
pre = cur;
23+
cur = pre->next;
24+
}
25+
return dummy->next;
1926
}
20-
};
21-
22-
27+
};

solution/0000-0099/0024.Swap Nodes in Pairs/Solution.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ public ListNode swapPairs(ListNode head) {
1313
ListNode dummy = new ListNode(0, head);
1414
ListNode pre = dummy, cur = head;
1515
while (cur != null && cur.next != null) {
16-
pre.next = cur.next;
17-
ListNode t = cur.next.next;
18-
cur.next.next = cur;
19-
cur.next = t;
16+
ListNode t = cur.next;
17+
cur.next = t.next;
18+
t.next = cur;
19+
pre.next = t;
2020
pre = cur;
21-
cur = cur.next;
21+
cur = pre.next;
22+
2223
}
2324
return dummy.next;
2425
}

solution/0000-0099/0024.Swap Nodes in Pairs/Solution.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ def swapPairs(self, head: ListNode) -> ListNode:
88
dummy = ListNode(next=head)
99
pre, cur = dummy, head
1010
while cur and cur.next:
11-
pre.next = cur.next
12-
t = cur.next.next
13-
cur.next.next = cur
14-
cur.next = t
11+
t = cur.next
12+
cur.next = t.next
13+
t.next = cur
14+
pre.next = t
1515
pre = cur
16-
cur = cur.next
16+
cur = pre.next
1717
return dummy.next

0 commit comments

Comments
 (0)