-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindKthFromTail15.java
More file actions
74 lines (69 loc) · 1.92 KB
/
FindKthFromTail15.java
File metadata and controls
74 lines (69 loc) · 1.92 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package offer;
/*
* p 与q的距离就差k-1
*/
public class FindKthFromTail15 {
public static ListNode FindKthToTail(ListNode head, int k) {
ListNode p = head, q = head;
if(head == null || k== 0){
return null;
}
//走 k-1 步子
while(--k> 0){
//走到k-1步,就到达链表尾部 return null
if(p.next == null) return null;
p = p.next;
}
//p.next == null 说明p到达最后一个节点,那么q就到了倒数第k个节点
while(p.next != null){
p = p.next;
q = q.next;
}
return q;
}
//建立链表 头插法 与插入顺序相反
public static ListNode InsertFromHead(ListNode head) {
//ͷ�巨
for (int i = 0; i < 5; i++) {
ListNode node = new ListNode(i);
if(i == 0){
head = node;
}else{
node.next = head.next;
head.next = node;
}
}
ListNode q = head;
while(q != null){
System.out.println(q.val);
q = q.next;
}
return head;
}
//建立链表 尾插法:等同插入顺序
public static ListNode InsertFromTail(ListNode head){
ListNode q = head;
for (int i = 0; i < 5; i++) {
ListNode node = new ListNode(i);
if(i == 0){
head = node;
q = head;
}else{
q.next = node;
q = node;
}
}
ListNode p = head;
while(p != null){
System.out.println(p.val);
p = p.next;
}
return head;
}
public static void main(String[] args) {
ListNode head = null;
head = InsertFromTail(head);
ListNode l = FindKthToTail(head, 6);
System.out.println(l.val);
}
}