算法汇总
以下是所有算法汇总,包括GitHub源码地址链接:力扣算法练习汇总(持续更新…)
题目
剑指 Offer 22. 链表中倒数第k个节点
关键点
1、
2、
代码
1.解体方法
思路
代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
// 1. node size
int nodeCount = 0;
ListNode temp = head;
while (temp != null) {
nodeCount++;
temp = temp.next;
}
// 2. target node position
int position = nodeCount - k + 1;
temp = head;
nodeCount = 0;
while (temp != null) {
nodeCount++;
if (nodeCount == position) {
return temp;
}
temp = temp.next;
}
return null;
}
}
时间和空间复杂度
2.解题方法,如暴力法
思路
代码
时间和空间复杂度