Skip to content

Commit 786ba6d

Browse files
committed
feat: add solutions to lcci problem: No.02.02. Kth Node From End of List
1 parent 07f7bf0 commit 786ba6d

File tree

7 files changed

+112
-38
lines changed

7 files changed

+112
-38
lines changed

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

+39-11
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141

4242
class Solution:
4343
def kthToLast(self, head: ListNode, k: int) -> int:
44-
p = q = head
44+
slow = fast = head
4545
for _ in range(k):
46-
q = q.next
47-
while q:
48-
p, q = p.next, q.next
49-
return p.val
46+
fast = fast.next
47+
while fast:
48+
slow, fast = slow.next, fast.next
49+
return slow.val
5050
```
5151

5252
### **Java**
@@ -64,15 +64,15 @@ class Solution:
6464
*/
6565
class Solution {
6666
public int kthToLast(ListNode head, int k) {
67-
ListNode p = head, q = head;
67+
ListNode slow = head, fast = head;
6868
while (k-- > 0) {
69-
q = q.next;
69+
fast = fast.next;
7070
}
71-
while (q != null) {
72-
q = q.next;
73-
p = p.next;
71+
while (fast != null) {
72+
slow = slow.next;
73+
fast = fast.next;
7474
}
75-
return p.val;
75+
return slow.val;
7676
}
7777
}
7878
```
@@ -105,6 +105,34 @@ var kthToLast = function(head, k) {
105105
};
106106
```
107107

108+
### **C++**
109+
110+
```cpp
111+
/**
112+
* Definition for singly-linked list.
113+
* struct ListNode {
114+
* int val;
115+
* ListNode *next;
116+
* ListNode(int x) : val(x), next(NULL) {}
117+
* };
118+
*/
119+
class Solution {
120+
public:
121+
int kthToLast(ListNode* head, int k) {
122+
ListNode* fast = head;
123+
ListNode* slow = head;
124+
while (k-- > 0) {
125+
fast = fast->next;
126+
}
127+
while (fast) {
128+
slow = slow->next;
129+
fast = fast->next;
130+
}
131+
return slow->val;
132+
}
133+
};
134+
```
135+
108136
### **...**
109137
110138
```

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

+39-11
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535

3636
class Solution:
3737
def kthToLast(self, head: ListNode, k: int) -> int:
38-
p = q = head
38+
slow = fast = head
3939
for _ in range(k):
40-
q = q.next
41-
while q:
42-
p, q = p.next, q.next
43-
return p.val
40+
fast = fast.next
41+
while fast:
42+
slow, fast = slow.next, fast.next
43+
return slow.val
4444
```
4545

4646
### **Java**
@@ -56,15 +56,15 @@ class Solution:
5656
*/
5757
class Solution {
5858
public int kthToLast(ListNode head, int k) {
59-
ListNode p = head, q = head;
59+
ListNode slow = head, fast = head;
6060
while (k-- > 0) {
61-
q = q.next;
61+
fast = fast.next;
6262
}
63-
while (q != null) {
64-
q = q.next;
65-
p = p.next;
63+
while (fast != null) {
64+
slow = slow.next;
65+
fast = fast.next;
6666
}
67-
return p.val;
67+
return slow.val;
6868
}
6969
}
7070
```
@@ -97,6 +97,34 @@ var kthToLast = function(head, k) {
9797
};
9898
```
9999

100+
### **C++**
101+
102+
```cpp
103+
/**
104+
* Definition for singly-linked list.
105+
* struct ListNode {
106+
* int val;
107+
* ListNode *next;
108+
* ListNode(int x) : val(x), next(NULL) {}
109+
* };
110+
*/
111+
class Solution {
112+
public:
113+
int kthToLast(ListNode* head, int k) {
114+
ListNode* fast = head;
115+
ListNode* slow = head;
116+
while (k-- > 0) {
117+
fast = fast->next;
118+
}
119+
while (fast) {
120+
slow = slow->next;
121+
fast = fast->next;
122+
}
123+
return slow->val;
124+
}
125+
};
126+
```
127+
100128
### **...**
101129
102130
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
int kthToLast(ListNode* head, int k) {
12+
ListNode* fast = head;
13+
ListNode* slow = head;
14+
while (k-- > 0) {
15+
fast = fast->next;
16+
}
17+
while (fast) {
18+
slow = slow->next;
19+
fast = fast->next;
20+
}
21+
return slow->val;
22+
}
23+
};

lcci/02.02.Kth Node From End of List/Solution.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
*/
99
class Solution {
1010
public int kthToLast(ListNode head, int k) {
11-
ListNode p = head, q = head;
11+
ListNode slow = head, fast = head;
1212
while (k-- > 0) {
13-
q = q.next;
13+
fast = fast.next;
1414
}
15-
while (q != null) {
16-
q = q.next;
17-
p = p.next;
15+
while (fast != null) {
16+
slow = slow.next;
17+
fast = fast.next;
1818
}
19-
return p.val;
19+
return slow.val;
2020
}
2121
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
class Solution:
88
def kthToLast(self, head: ListNode, k: int) -> int:
9-
p = q = head
9+
slow = fast = head
1010
for _ in range(k):
11-
q = q.next
12-
while q:
13-
p, q = p.next, q.next
14-
return p.val
11+
fast = fast.next
12+
while fast:
13+
slow, fast = slow.next, fast.next
14+
return slow.val

lcof/面试题44. 数字序列中某一位的数字/README.md

-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ class Solution:
6060
p += 1
6161
num = n // (p + 1) + pow(10, p)
6262
return int(str(num)[n % (p + 1)])
63-
64-
65-
6663
```
6764

6865
### **Java**

lcof/面试题44. 数字序列中某一位的数字/Solution.py

-2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,3 @@ def get_bit_num():
1313
p += 1
1414
num = n // (p + 1) + pow(10, p)
1515
return int(str(num)[n % (p + 1)])
16-
17-

0 commit comments

Comments
 (0)