Skip to content

Commit 27a39cf

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0024
1 parent d0112c9 commit 27a39cf

File tree

11 files changed

+89
-51
lines changed

11 files changed

+89
-51
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
1. [从尾到头打印链表](/lcof/面试题06.%20从尾到头打印链表/README.md)
5959
1. [删除链表的节点](/lcof/面试题18.%20删除链表的节点/README.md)
6060
1. [链表中倒数第 k 个节点](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README.md)
61+
1. [两两交换链表中的节点](/solution/0000-0099/0024.Swap%20Nodes%20in%20Pairs/README.md)
6162
1. [合并两个有序链表](/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/README.md)
6263
1. [合并 K 个排序链表](/solution/0000-0099/0023.Merge%20k%20Sorted%20Lists/README.md)
6364
1. [排序链表](/solution/0100-0199/0148.Sort%20List/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
5656

5757
1. [Delete Node in a Linked List](/solution/0200-0299/0237.Delete%20Node%20in%20a%20Linked%20List/README_EN.md)
5858
1. [Kth Node From End of List](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README_EN.md)
59+
1. [Swap Nodes in Pairs](/solution/0000-0099/0024.Swap%20Nodes%20in%20Pairs/README_EN.md)
5960
1. [Merge Two Sorted Lists](/solution/0000-0099/0021.Merge%20Two%20Sorted%20Lists/README_EN.md)
6061
1. [Merge k Sorted Lists](/solution/0000-0099/0023.Merge%20k%20Sorted%20Lists/README_EN.md)
6162
1. [Sort List](/solution/0100-0199/0148.Sort%20List/README_EN.md)

index.html

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
search: [
3636
'/','/solution/','/lcof/','/lcci/', '/basic/'
3737
],
38+
loadSidebar: 'summary.md',
3839
auto2top: true,
3940
subMaxLevel: 2,
4041
alias: {

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

+21-4
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,16 @@
3434
# self.next = next
3535
class Solution:
3636
def swapPairs(self, head: ListNode) -> ListNode:
37-
if head is None or head.next is None:
38-
return head
39-
37+
dummy = ListNode(next=head)
38+
pre, cur = dummy, head
39+
while cur and cur.next:
40+
pre.next = cur.next
41+
t = cur.next.next
42+
cur.next.next = cur
43+
cur.next = t
44+
pre = cur
45+
cur = cur.next
46+
return dummy.next
4047
```
4148

4249
### **Java**
@@ -56,7 +63,17 @@ class Solution:
5663
*/
5764
class Solution {
5865
public ListNode swapPairs(ListNode head) {
59-
66+
ListNode dummy = new ListNode(0, head);
67+
ListNode pre = dummy, cur = head;
68+
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;
73+
pre = cur;
74+
cur = cur.next;
75+
}
76+
return dummy.next;
6077
}
6178
}
6279
```

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ Given <code>1-&gt;2-&gt;3-&gt;4</code>, you should return the list as <code>2-&g
3232
# self.next = next
3333
class Solution:
3434
def swapPairs(self, head: ListNode) -> ListNode:
35+
dummy = ListNode(next=head)
36+
pre, cur = dummy, head
37+
while cur and cur.next:
38+
pre.next = cur.next
39+
t = cur.next.next
40+
cur.next.next = cur
41+
cur.next = t
42+
pre = cur
43+
cur = cur.next
44+
return dummy.next
3545
```
3646

3747
### **Java**
@@ -49,7 +59,17 @@ class Solution:
4959
*/
5060
class Solution {
5161
public ListNode swapPairs(ListNode head) {
52-
62+
ListNode dummy = new ListNode(0, head);
63+
ListNode pre = dummy, cur = head;
64+
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;
69+
pre = cur;
70+
cur = cur.next;
71+
}
72+
return dummy.next;
5373
}
5474
}
5575
```
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
111
class Solution {
212
public ListNode swapPairs(ListNode head) {
3-
if (head == null || head.next == null) {
4-
return head;
13+
ListNode dummy = new ListNode(0, head);
14+
ListNode pre = dummy, cur = head;
15+
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;
20+
pre = cur;
21+
cur = cur.next;
522
}
6-
ListNode pre = head.next;
7-
ListNode p = head;
8-
ListNode q = head.next;
9-
10-
while (q != null) {
11-
ListNode t = q.next;
12-
q.next = p;
13-
if (t == null || t.next == null) {
14-
p.next = t;
15-
break;
16-
}
17-
p.next = t.next;
18-
p = t;
19-
q = p.next;
20-
}
21-
22-
return pre;
23+
return dummy.next;
2324
}
2425
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
# Definition for singly-linked list.
22
# class ListNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.next = None
6-
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
76
class Solution:
8-
def swapPairs(self, head):
9-
"""
10-
:type head: ListNode
11-
:rtype: ListNode
12-
"""
13-
if (not head) or (not head.next):
14-
return head
15-
pre=head.next
16-
p=head
17-
q=head.next
18-
19-
while q:
20-
t=q.next
21-
q.next=p
22-
if (not t) or (not t.next):
23-
p.next=t
24-
break
25-
p.next=t.next
26-
p=t
27-
q=p.next
28-
return pre
7+
def swapPairs(self, head: ListNode) -> ListNode:
8+
dummy = ListNode(next=head)
9+
pre, cur = dummy, head
10+
while cur and cur.next:
11+
pre.next = cur.next
12+
t = cur.next.next
13+
cur.next.next = cur
14+
cur.next = t
15+
pre = cur
16+
cur = cur.next
17+
return dummy.next

solution/0200-0299/0206.Reverse Linked List/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ var reverseList = function (head) {
125125

126126
### **Go**
127127

128-
```Go
128+
```go
129129
func reverseList(head *ListNode) *ListNode {
130130
if head == nil ||head.Next == nil {
131131
return head

solution/0200-0299/0206.Reverse Linked List/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ var reverseList = function (head) {
124124

125125
### **Go**
126126

127-
```Go
127+
```go
128128
func reverseList(head *ListNode) *ListNode {
129129
if head == nil ||head.Next == nil {
130130
return head

summary.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- 题解速览
2+
- [LeetCode(未完)](/solution/README.md)
3+
- [LeetCode 《剑指 Offer(第 2 版)》](/lcof/README.md)
4+
- [LeetCode 《程序员面试金典(第 6 版)》](/lcci/README.md)

summary_en.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- All Solutions
2+
- [LeetCode(Not finished yet)](/solution/README_EN.md)
3+
- [LCOF: _Coding Interviews, 2nd Edition_](/lcof/README_EN.md)
4+
- [LCCI: _Cracking the Coding Interview, 6th Edition_](/lcci/README_EN.md)

0 commit comments

Comments
 (0)