Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 08f9dc0

Browse files
feat: add solutions to lcci problem: No.02.02 (doocs#2306)
No.02.02.Kth Node From End of List
1 parent 3aff15b commit 08f9dc0

File tree

7 files changed

+130
-26
lines changed

7 files changed

+130
-26
lines changed

‎lcci/02.02.Kth Node From End of List/README.md‎

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020

2121
## 解法
2222

23-
### 方法一
23+
### 方法一:快慢指针
24+
25+
我们定义两个指针 `slow``fast`,初始时都指向链表头节点 `head`。然后 `fast` 指针先向前移动 $k$ 步,然后 `slow``fast` 指针同时向前移动,直到 `fast` 指针指向链表末尾。此时 `slow` 指针指向的节点就是倒数第 $k$ 个节点。
26+
27+
时间复杂度 $O(n),ドル其中 $n$ 是链表的长度。空间复杂度 $O(1)$。
2428

2529
<!-- tabs:start -->
2630

@@ -38,7 +42,8 @@ class Solution:
3842
for _ in range(k):
3943
fast = fast.next
4044
while fast:
41-
slow, fast = slow.next, fast.next
45+
slow = slow.next
46+
fast = fast.next
4247
return slow.val
4348
```
4449

@@ -80,7 +85,7 @@ public:
8085
int kthToLast(ListNode* head, int k) {
8186
ListNode* fast = head;
8287
ListNode* slow = head;
83-
while (k-- > 0) {
88+
while (k--) {
8489
fast = fast->next;
8590
}
8691
while (fast) {
@@ -93,9 +98,16 @@ public:
9398
```
9499
95100
```go
101+
/**
102+
* Definition for singly-linked list.
103+
* type ListNode struct {
104+
* Val int
105+
* Next *ListNode
106+
* }
107+
*/
96108
func kthToLast(head *ListNode, k int) int {
97109
slow, fast := head, head
98-
for i := 0; i < k; i++ {
110+
for ; k > 0; k-- {
99111
fast = fast.Next
100112
}
101113
for fast != nil {
@@ -106,6 +118,32 @@ func kthToLast(head *ListNode, k int) int {
106118
}
107119
```
108120

121+
```ts
122+
/**
123+
* Definition for singly-linked list.
124+
* class ListNode {
125+
* val: number
126+
* next: ListNode | null
127+
* constructor(val?: number, next?: ListNode | null) {
128+
* this.val = (val===undefined ? 0 : val)
129+
* this.next = (next===undefined ? null : next)
130+
* }
131+
* }
132+
*/
133+
134+
function kthToLast(head: ListNode | null, k: number): number {
135+
let [slow, fast] = [head, head];
136+
while (k--) {
137+
fast = fast.next;
138+
}
139+
while (fast !== null) {
140+
slow = slow.next;
141+
fast = fast.next;
142+
}
143+
return slow.val;
144+
}
145+
```
146+
109147
```rust
110148
// Definition for singly-linked list.
111149
// #[derive(PartialEq, Eq, Clone, Debug)]
@@ -153,14 +191,13 @@ impl Solution {
153191
* @return {number}
154192
*/
155193
var kthToLast = function (head, k) {
156-
let fast = head,
157-
slow = head;
158-
for (let i = 0; i < k; i++) {
194+
let [slow, fast] = [head, head];
195+
while (k--) {
159196
fast = fast.next;
160197
}
161-
while (fast != null) {
162-
fast = fast.next;
198+
while (fast !== null) {
163199
slow = slow.next;
200+
fast = fast.next;
164201
}
165202
return slow.val;
166203
};

‎lcci/02.02.Kth Node From End of List/README_EN.md‎

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222

2323
## Solutions
2424

25-
### Solution 1
25+
### Solution 1: Two Pointers
26+
27+
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.
28+
29+
The time complexity is $O(n),ドル where $n$ is the length of the list. The space complexity is $O(1)$.
2630

2731
<!-- tabs:start -->
2832

@@ -40,7 +44,8 @@ class Solution:
4044
for _ in range(k):
4145
fast = fast.next
4246
while fast:
43-
slow, fast = slow.next, fast.next
47+
slow = slow.next
48+
fast = fast.next
4449
return slow.val
4550
```
4651

@@ -82,7 +87,7 @@ public:
8287
int kthToLast(ListNode* head, int k) {
8388
ListNode* fast = head;
8489
ListNode* slow = head;
85-
while (k-- > 0) {
90+
while (k--) {
8691
fast = fast->next;
8792
}
8893
while (fast) {
@@ -95,9 +100,16 @@ public:
95100
```
96101
97102
```go
103+
/**
104+
* Definition for singly-linked list.
105+
* type ListNode struct {
106+
* Val int
107+
* Next *ListNode
108+
* }
109+
*/
98110
func kthToLast(head *ListNode, k int) int {
99111
slow, fast := head, head
100-
for i := 0; i < k; i++ {
112+
for ; k > 0; k-- {
101113
fast = fast.Next
102114
}
103115
for fast != nil {
@@ -108,6 +120,32 @@ func kthToLast(head *ListNode, k int) int {
108120
}
109121
```
110122

123+
```ts
124+
/**
125+
* Definition for singly-linked list.
126+
* class ListNode {
127+
* val: number
128+
* next: ListNode | null
129+
* constructor(val?: number, next?: ListNode | null) {
130+
* this.val = (val===undefined ? 0 : val)
131+
* this.next = (next===undefined ? null : next)
132+
* }
133+
* }
134+
*/
135+
136+
function kthToLast(head: ListNode | null, k: number): number {
137+
let [slow, fast] = [head, head];
138+
while (k--) {
139+
fast = fast.next;
140+
}
141+
while (fast !== null) {
142+
slow = slow.next;
143+
fast = fast.next;
144+
}
145+
return slow.val;
146+
}
147+
```
148+
111149
```rust
112150
// Definition for singly-linked list.
113151
// #[derive(PartialEq, Eq, Clone, Debug)]
@@ -155,14 +193,13 @@ impl Solution {
155193
* @return {number}
156194
*/
157195
var kthToLast = function (head, k) {
158-
let fast = head,
159-
slow = head;
160-
for (let i = 0; i < k; i++) {
196+
let [slow, fast] = [head, head];
197+
while (k--) {
161198
fast = fast.next;
162199
}
163-
while (fast != null) {
164-
fast = fast.next;
200+
while (fast !== null) {
165201
slow = slow.next;
202+
fast = fast.next;
166203
}
167204
return slow.val;
168205
};

‎lcci/02.02.Kth Node From End of List/Solution.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Solution {
1111
int kthToLast(ListNode* head, int k) {
1212
ListNode* fast = head;
1313
ListNode* slow = head;
14-
while (k-- > 0) {
14+
while (k--) {
1515
fast = fast->next;
1616
}
1717
while (fast) {

‎lcci/02.02.Kth Node From End of List/Solution.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
18
func kthToLast(head *ListNode, k int) int {
29
slow, fast := head, head
3-
for i:=0; i<k; i++ {
10+
for ; k>0; k-- {
411
fast = fast.Next
512
}
613
for fast != nil {

‎lcci/02.02.Kth Node From End of List/Solution.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
* @return {number}
1212
*/
1313
var kthToLast = function (head, k) {
14-
let fast = head,
15-
slow = head;
16-
for (let i = 0; i < k; i++) {
14+
let [slow, fast] = [head, head];
15+
while (k--) {
1716
fast = fast.next;
1817
}
19-
while (fast != null) {
20-
fast = fast.next;
18+
while (fast !== null) {
2119
slow = slow.next;
20+
fast = fast.next;
2221
}
2322
return slow.val;
2423
};

‎lcci/02.02.Kth Node From End of List/Solution.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ def kthToLast(self, head: ListNode, k: int) -> int:
1111
for _ in range(k):
1212
fast = fast.next
1313
while fast:
14-
slow, fast = slow.next, fast.next
14+
slow = slow.next
15+
fast = fast.next
1516
return slow.val
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function kthToLast(head: ListNode | null, k: number): number {
14+
let [slow, fast] = [head, head];
15+
while (k--) {
16+
fast = fast.next;
17+
}
18+
while (fast !== null) {
19+
slow = slow.next;
20+
fast = fast.next;
21+
}
22+
return slow.val;
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /