File tree 7 files changed +112
-38
lines changed
lcci/02.02.Kth Node From End of List
7 files changed +112
-38
lines changed Original file line number Diff line number Diff line change 41
41
42
42
class Solution :
43
43
def kthToLast (self , head : ListNode, k : int ) -> int :
44
- p = q = head
44
+ slow = fast = head
45
45
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
50
50
```
51
51
52
52
### ** Java**
@@ -64,15 +64,15 @@ class Solution:
64
64
*/
65
65
class Solution {
66
66
public int kthToLast (ListNode head , int k ) {
67
- ListNode p = head, q = head;
67
+ ListNode slow = head, fast = head;
68
68
while (k-- > 0 ) {
69
- q = q . next;
69
+ fast = fast . next;
70
70
}
71
- while (q != null ) {
72
- q = q . next;
73
- p = p . next;
71
+ while (fast != null ) {
72
+ slow = slow . next;
73
+ fast = fast . next;
74
74
}
75
- return p . val;
75
+ return slow . val;
76
76
}
77
77
}
78
78
```
@@ -105,6 +105,34 @@ var kthToLast = function(head, k) {
105
105
};
106
106
```
107
107
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
+
108
136
### **...**
109
137
110
138
```
Original file line number Diff line number Diff line change 35
35
36
36
class Solution :
37
37
def kthToLast (self , head : ListNode, k : int ) -> int :
38
- p = q = head
38
+ slow = fast = head
39
39
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
44
44
```
45
45
46
46
### ** Java**
@@ -56,15 +56,15 @@ class Solution:
56
56
*/
57
57
class Solution {
58
58
public int kthToLast (ListNode head , int k ) {
59
- ListNode p = head, q = head;
59
+ ListNode slow = head, fast = head;
60
60
while (k-- > 0 ) {
61
- q = q . next;
61
+ fast = fast . next;
62
62
}
63
- while (q != null ) {
64
- q = q . next;
65
- p = p . next;
63
+ while (fast != null ) {
64
+ slow = slow . next;
65
+ fast = fast . next;
66
66
}
67
- return p . val;
67
+ return slow . val;
68
68
}
69
69
}
70
70
```
@@ -97,6 +97,34 @@ var kthToLast = function(head, k) {
97
97
};
98
98
```
99
99
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
+
100
128
### **...**
101
129
102
130
```
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change 8
8
*/
9
9
class Solution {
10
10
public int kthToLast (ListNode head , int k ) {
11
- ListNode p = head , q = head ;
11
+ ListNode slow = head , fast = head ;
12
12
while (k -- > 0 ) {
13
- q = q .next ;
13
+ fast = fast .next ;
14
14
}
15
- while (q != null ) {
16
- q = q .next ;
17
- p = p .next ;
15
+ while (fast != null ) {
16
+ slow = slow .next ;
17
+ fast = fast .next ;
18
18
}
19
- return p .val ;
19
+ return slow .val ;
20
20
}
21
21
}
Original file line number Diff line number Diff line change 6
6
7
7
class Solution :
8
8
def kthToLast (self , head : ListNode , k : int ) -> int :
9
- p = q = head
9
+ slow = fast = head
10
10
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
Original file line number Diff line number Diff line change @@ -60,9 +60,6 @@ class Solution:
60
60
p += 1
61
61
num = n // (p + 1 ) + pow (10 , p)
62
62
return int (str (num)[n % (p + 1 )])
63
-
64
-
65
-
66
63
```
67
64
68
65
### ** Java**
Original file line number Diff line number Diff line change @@ -13,5 +13,3 @@ def get_bit_num():
13
13
p += 1
14
14
num = n // (p + 1 ) + pow (10 , p )
15
15
return int (str (num )[n % (p + 1 )])
16
-
17
-
You can’t perform that action at this time.
0 commit comments