Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lcci problem: No.02.02 #2306

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions lcci/02.02.Kth Node From End of List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

## 解法

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

我们定义两个指针 `slow` 和 `fast`,初始时都指向链表头节点 `head`。然后 `fast` 指针先向前移动 $k$ 步,然后 `slow` 和 `fast` 指针同时向前移动,直到 `fast` 指针指向链表末尾。此时 `slow` 指针指向的节点就是倒数第 $k$ 个节点。

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

<!-- tabs:start -->

Expand All @@ -38,7 +42,8 @@ class Solution:
for _ in range(k):
fast = fast.next
while fast:
slow, fast = slow.next, fast.next
slow = slow.next
fast = fast.next
return slow.val
```

Expand Down Expand Up @@ -80,7 +85,7 @@ public:
int kthToLast(ListNode* head, int k) {
ListNode* fast = head;
ListNode* slow = head;
while (k-- > 0) {
while (k--) {
fast = fast->next;
}
while (fast) {
Expand All @@ -93,9 +98,16 @@ public:
```

```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func kthToLast(head *ListNode, k int) int {
slow, fast := head, head
for i := 0; i < k; i++ {
for ; k > 0; k-- {
fast = fast.Next
}
for fast != nil {
Expand All @@ -106,6 +118,32 @@ func kthToLast(head *ListNode, k int) int {
}
```

```ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function kthToLast(head: ListNode | null, k: number): number {
let [slow, fast] = [head, head];
while (k--) {
fast = fast.next;
}
while (fast !== null) {
slow = slow.next;
fast = fast.next;
}
return slow.val;
}
```

```rust
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
Expand Down Expand Up @@ -153,14 +191,13 @@ impl Solution {
* @return {number}
*/
var kthToLast = function (head, k) {
let fast = head,
slow = head;
for (let i = 0; i < k; i++) {
let [slow, fast] = [head, head];
while (k--) {
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
while (fast !== null) {
slow = slow.next;
fast = fast.next;
}
return slow.val;
};
Expand Down
55 changes: 46 additions & 9 deletions lcci/02.02.Kth Node From End of List/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

## Solutions

### Solution 1
### Solution 1: Two Pointers

We define two pointers `slow` and `fast`, both initially pointing to the head node `head`. Then the `fast` pointer moves forward $k$ steps first, and then the `slow` and `fast` pointers move forward together until the `fast` pointer points to the end of the list. At this point, the node pointed to by the `slow` pointer is the $k$-th node from the end of the list.

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

<!-- tabs:start -->

Expand All @@ -40,7 +44,8 @@ class Solution:
for _ in range(k):
fast = fast.next
while fast:
slow, fast = slow.next, fast.next
slow = slow.next
fast = fast.next
return slow.val
```

Expand Down Expand Up @@ -82,7 +87,7 @@ public:
int kthToLast(ListNode* head, int k) {
ListNode* fast = head;
ListNode* slow = head;
while (k-- > 0) {
while (k--) {
fast = fast->next;
}
while (fast) {
Expand All @@ -95,9 +100,16 @@ public:
```

```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func kthToLast(head *ListNode, k int) int {
slow, fast := head, head
for i := 0; i < k; i++ {
for ; k > 0; k-- {
fast = fast.Next
}
for fast != nil {
Expand All @@ -108,6 +120,32 @@ func kthToLast(head *ListNode, k int) int {
}
```

```ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function kthToLast(head: ListNode | null, k: number): number {
let [slow, fast] = [head, head];
while (k--) {
fast = fast.next;
}
while (fast !== null) {
slow = slow.next;
fast = fast.next;
}
return slow.val;
}
```

```rust
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
Expand Down Expand Up @@ -155,14 +193,13 @@ impl Solution {
* @return {number}
*/
var kthToLast = function (head, k) {
let fast = head,
slow = head;
for (let i = 0; i < k; i++) {
let [slow, fast] = [head, head];
while (k--) {
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
while (fast !== null) {
slow = slow.next;
fast = fast.next;
}
return slow.val;
};
Expand Down
2 changes: 1 addition & 1 deletion lcci/02.02.Kth Node From End of List/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Solution {
int kthToLast(ListNode* head, int k) {
ListNode* fast = head;
ListNode* slow = head;
while (k-- > 0) {
while (k--) {
fast = fast->next;
}
while (fast) {
Expand Down
9 changes: 8 additions & 1 deletion lcci/02.02.Kth Node From End of List/Solution.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func kthToLast(head *ListNode, k int) int {
slow, fast := head, head
for i := 0; i < k; i++ {
for ; k > 0; k-- {
fast = fast.Next
}
for fast != nil {
Expand Down
9 changes: 4 additions & 5 deletions lcci/02.02.Kth Node From End of List/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
* @return {number}
*/
var kthToLast = function (head, k) {
let fast = head,
slow = head;
for (let i = 0; i < k; i++) {
let [slow, fast] = [head, head];
while (k--) {
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
while (fast !== null) {
slow = slow.next;
fast = fast.next;
}
return slow.val;
};
3 changes: 2 additions & 1 deletion lcci/02.02.Kth Node From End of List/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ def kthToLast(self, head: ListNode, k: int) -> int:
for _ in range(k):
fast = fast.next
while fast:
slow, fast = slow.next, fast.next
slow = slow.next
fast = fast.next
return slow.val
23 changes: 23 additions & 0 deletions lcci/02.02.Kth Node From End of List/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function kthToLast(head: ListNode | null, k: number): number {
let [slow, fast] = [head, head];
while (k--) {
fast = fast.next;
}
while (fast !== null) {
slow = slow.next;
fast = fast.next;
}
return slow.val;
}