Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1721 (doocs#1846)
Browse files Browse the repository at this point in the history
No.1721.Swapping Nodes in a Linked List
  • Loading branch information
yanglbme authored Oct 19, 2023
1 parent c64265d commit 588b31c
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 119 deletions.
80 changes: 60 additions & 20 deletions solution/1700-1799/1721.Swapping Nodes in a Linked List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@

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

**方法一:快慢指针**

我们可以先用快指针 $fast$ 找到链表的第 $k$ 个节点,用指针 $p$ 指向它。然后我们再用慢指针 $slow$ 从链表的头节点出发,快慢指针同时向后移动,当快指针到达链表的最后一个节点时,慢指针 $slow$ 恰好指向倒数第 $k$ 个节点,用指针 $q$ 指向它。此时,我们只需要交换 $p$ 和 $q$ 的值即可。

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

<!-- tabs:start -->

### **Python3**
Expand All @@ -74,14 +80,13 @@
# self.val = val
# self.next = next
class Solution:
def swapNodes(self, head: ListNode, k: int) -> ListNode:
fast = head
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
fast = slow = head
for _ in range(k - 1):
fast = fast.next
p = fast
slow = head
while fast.next:
slow, fast = slow.next, fast.next
fast, slow = fast.next, slow.next
q = slow
p.val, q.val = q.val, p.val
return head
Expand Down Expand Up @@ -111,8 +116,8 @@ class Solution {
ListNode p = fast;
ListNode slow = head;
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
slow = slow.next;
}
ListNode q = slow;
int t = p.val;
Expand All @@ -139,16 +144,18 @@ class Solution {
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
ListNode* p1 = head;
for (int i = 1; i < k; i++)
p1 = p1->next;
ListNode *slow = head, *fast = p1->next;

while (fast) {
ListNode* fast = head;
while (--k) {
fast = fast->next;
}
ListNode* slow = head;
ListNode* p = fast;
while (fast->next) {
fast = fast->next;
slow = slow->next;
}
swap(slow->val, p1->val);
ListNode* q = slow;
swap(p->val, q->val);
return head;
}
};
Expand All @@ -166,14 +173,13 @@ public:
*/
func swapNodes(head *ListNode, k int) *ListNode {
fast := head
for k > 1 {
for ; k > 1; k-- {
fast = fast.Next
k--
}
p := fast
slow := head
for fast.Next != nil {
slow, fast = slow.Next, fast.Next
fast, slow = fast.Next, slow.Next
}
q := slow
p.Val, q.Val = q.Val, p.Val
Expand All @@ -197,22 +203,56 @@ func swapNodes(head *ListNode, k int) *ListNode {
*/

function swapNodes(head: ListNode | null, k: number): ListNode | null {
let fast = head;
let [fast, slow] = [head, head];
while (--k) {
fast = fast.next;
}
let p = fast;
let slow = head;
const p = fast;
while (fast.next) {
slow = slow.next;
fast = fast.next;
slow = slow.next;
}
let q = slow;
const q = slow;
[p.val, q.val] = [q.val, p.val];
return 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 SwapNodes(ListNode head, int k) {
ListNode fast = head;
while (--k > 0) {
fast = fast.next;
}
ListNode p = fast;
ListNode slow = head;
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
ListNode q = slow;
int t = p.val;
p.val = q.val;
q.val = t;
return head;
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@

## Solutions

**Solution 1: Two Pointers**

We can first use a fast pointer `fast` to find the $k$th node of the linked list, and use a pointer `p` to point to it. Then, we use a slow pointer `slow` to start from the head node of the linked list, and move both pointers forward at the same time. When the fast pointer reaches the last node of the linked list, the slow pointer `slow` points to the $k$th node from the end of the linked list, and we use a pointer `q` to point to it. At this point, we only need to swap the values of `p` and `q`.

The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand All @@ -45,14 +51,13 @@
# self.val = val
# self.next = next
class Solution:
def swapNodes(self, head: ListNode, k: int) -> ListNode:
fast = head
def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
fast = slow = head
for _ in range(k - 1):
fast = fast.next
p = fast
slow = head
while fast.next:
slow, fast = slow.next, fast.next
fast, slow = fast.next, slow.next
q = slow
p.val, q.val = q.val, p.val
return head
Expand Down Expand Up @@ -80,8 +85,8 @@ class Solution {
ListNode p = fast;
ListNode slow = head;
while (fast.next != null) {
slow = slow.next;
fast = fast.next;
slow = slow.next;
}
ListNode q = slow;
int t = p.val;
Expand All @@ -108,16 +113,18 @@ class Solution {
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
ListNode* p1 = head;
for (int i = 1; i < k; i++)
p1 = p1->next;
ListNode *slow = head, *fast = p1->next;

while (fast) {
ListNode* fast = head;
while (--k) {
fast = fast->next;
}
ListNode* slow = head;
ListNode* p = fast;
while (fast->next) {
fast = fast->next;
slow = slow->next;
}
swap(slow->val, p1->val);
ListNode* q = slow;
swap(p->val, q->val);
return head;
}
};
Expand All @@ -135,14 +142,13 @@ public:
*/
func swapNodes(head *ListNode, k int) *ListNode {
fast := head
for k > 1 {
for ; k > 1; k-- {
fast = fast.Next
k--
}
p := fast
slow := head
for fast.Next != nil {
slow, fast = slow.Next, fast.Next
fast, slow = fast.Next, slow.Next
}
q := slow
p.Val, q.Val = q.Val, p.Val
Expand All @@ -166,22 +172,56 @@ func swapNodes(head *ListNode, k int) *ListNode {
*/

function swapNodes(head: ListNode | null, k: number): ListNode | null {
let fast = head;
let [fast, slow] = [head, head];
while (--k) {
fast = fast.next;
}
let p = fast;
let slow = head;
const p = fast;
while (fast.next) {
slow = slow.next;
fast = fast.next;
slow = slow.next;
}
let q = slow;
const q = slow;
[p.val, q.val] = [q.val, p.val];
return 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 SwapNodes(ListNode head, int k) {
ListNode fast = head;
while (--k > 0) {
fast = fast.next;
}
ListNode p = fast;
ListNode slow = head;
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
ListNode q = slow;
int t = p.val;
p.val = q.val;
q.val = t;
return head;
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
ListNode* p1 = head;
for (int i = 1; i < k; i++)
p1 = p1->next;
ListNode *slow = head, *fast = p1->next;

while (fast) {
fast = fast->next;
slow = slow->next;
}
swap(slow->val, p1->val);
return head;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapNodes(ListNode* head, int k) {
ListNode* fast = head;
while (--k) {
fast = fast->next;
}
ListNode* slow = head;
ListNode* p = fast;
while (fast->next) {
fast = fast->next;
slow = slow->next;
}
ListNode* q = slow;
swap(p->val, q->val);
return head;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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 SwapNodes(ListNode head, int k) {
ListNode fast = head;
while (--k > 0) {
fast = fast.next;
}
ListNode p = fast;
ListNode slow = head;
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
ListNode q = slow;
int t = p.val;
p.val = q.val;
q.val = t;
return head;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
*/
func swapNodes(head *ListNode, k int) *ListNode {
fast := head
for k > 1 {
for ; k > 1; k-- {
fast = fast.Next
k--
}
p := fast
slow := head
for fast.Next != nil {
slow, fast = slow.Next, fast.Next
fast, slow = fast.Next, slow.Next
}
q := slow
p.Val, q.Val = q.Val, p.Val
Expand Down
Loading

0 comments on commit 588b31c

Please sign in to comment.