-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindKthToTail.java
More file actions
56 lines (49 loc) · 1.65 KB
/
FindKthToTail.java
File metadata and controls
56 lines (49 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import org.testng.annotations.Test;
public class FindKthToTail {
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
// 输入一个链表,输出该链表中倒数第k个结点。
/*public ListNode FindKthToTail(ListNode head, int k) {
if (head == null) return null;
// 备注:要求只遍历一次就完成操作
ListNode tmp = head;
Stack<ListNode> mListNodeStack = new Stack<>();
while (tmp != null) {
mListNodeStack.push(tmp);
tmp = tmp.next;
}
for (int i = 0; i < k; i++) {
if (mListNodeStack.isEmpty()) return null;
tmp = mListNodeStack.pop();
}
return tmp;
}*/
// 备注:要求只遍历一次就完成操作
public ListNode FindKthToTail(ListNode head, int k) {
// 双指针做法,第一个指针先走 k - 1 步
// 然后,第二个指针和第一个指针开始同步走
// 当,第一个指针走到结束的时候,第二个指针恰好在倒数第 k 个结点处
if (head == null) return head;
ListNode iNode = head;
ListNode jNode = head;
if (k - 1 < 0) return null;
// i 指针向前走 k -1 步
for (int i = 0; i < k - 1; i++) {
iNode = iNode.next;
}
if (iNode == null) return iNode;
for (; iNode.next != null; ) {
iNode = iNode.next;
jNode = jNode.next;
}
return jNode;
}
@Test
public void test() {
}
}