-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathTest.java
More file actions
53 lines (45 loc) · 1022 Bytes
/
Test.java
File metadata and controls
53 lines (45 loc) · 1022 Bytes
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
package com.imood.msjava;
/**
* @description:
* @author: msJava
* @createDate: 2020/07/02
* @version: 1.0
*/
public class Test {
/**
* 输入单链表的倒数第N个元素
*
* @param head
* @param N
* @return
*/
public int kthToLast(ListNode head, int N) {
//双指针法
int a;
ListNode pre = head;
ListNode low = head;
if (head.next == null) {
return head.val;
}
//先让pre在单链表上走N步,然后两个一起走,
// pre到链表尾部时,low刚好位于链表倒数第N个节点上,直接返回该节点即可
for (int i = 0; i < N; i++) {
pre = pre.next;
}
while (pre != null) {
pre = pre.next;
low = low.next;
}
return low.val;
}
/**
* 单链表实体
*/
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
}