0%

剑指offer-22

题目

结果

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode p = head;
int length = 0;
while (p != null) {
length++;
p = p.next;
}
for (int i = 0; i < length - k; i++) {
head = head.next;
}
return head;
}
}

复杂度

时间复杂度:O(n)

空间复杂度:O(1)