import java.util.*;
public class LastKInLinkedList {
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public ListNode FindKthToTail(ListNode head,int k) {
if (k <= 0) {
return null;
}
if (head == null) {
return null;
}
ListNode fast = head;
ListNode slow = head;
int count = 0;
while (count != k-1) {
if (fast.next != null) {
fast = fast.next;
count++;
} else {
return null;
}
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
// 因为倒数第k个节点,与最后一个位置的节点相差 k-1 个元素 // 所以让 fast 先走 k-1 步,然后 fast 和 slow 同时走 // 直到 fast.next = null,即 fast 走到最后一个位置
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null || k <= 0)
return null;
ListNode fast = head;
ListNode slow = head;
while(k-1 > 0){
fast = fast.next;
if(fast == null)
return null;
k--;
}
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
ListNode cur = head;
int count = 0;
while(cur!= null){ //计算节点数
count++;
cur = cur.next;
}
if(k>count){ //无效节点位置
return null;
}
for(int i = 0; i<count-k;i++ ){ //找
head = head.next;
}
return head;
}
} public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if (head == null || k < 0) {
return null;
}
ListNode first = head;
ListNode second = head;
while (k > 0 && first != null) {
k--;
first = first.next;
}
while (first != null) {
first = first.next;
second = second.next;
}
return k > 0 ? null : second;
}
} /*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head==null){
return head;
}
if(k<=0){
return null;
}
ListNode fast=head;
ListNode show=head;
while(k-1>0){
fast=fast.next;
if(fast==null){
return null;
}
k--;
}
while(fast.next!=null){
fast=fast.next;
show=show.next;
}
return show;
}
} public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
ListNode dummy = new ListNode(0);
doFind(head, k, dummy);
return dummy.next;
}
private int doFind(ListNode head, int k, ListNode dummy){
if (head == null) return 0;
int num = doFind(head.next, k, dummy) + 1;
if (num == k) {
dummy.next = head;
}
return num;
}
} public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head==null||k==0) return null;
//快慢指针,先让快指针走k步,当快指针到达终点时慢指针的位置。
ListNode fast=head,slow=head;
int n=1;
while(fast!=null&&n<k){
fast=fast.next;
n++;
}
if(fast==null) return null;//k大于链表的元素数目
while(fast.next!=null){
fast=fast.next;
slow=slow.next;
}
return slow;
}
} /**
* 解题思路:使用快慢指针的思路。
*
* 快慢指针间隔k个节点,当快指针到达链表底部,则满指针到达倒数第k节点。
*
* @author freedom wang
* @date 2021-01-23 20:37:46
*/
public ListNode FindKthToTail(ListNode head, int k) {
if (head == null || k == 0) {
return null;
}
// 定义快慢指针
ListNode fast = head;
ListNode slow = head;
// 让快指针领先k个节点
for (int i = 0; i < k - 1; i++) {
if (fast.next != null) {
fast = fast.next;
} else {
return null;
}
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
//方法一:按照长度取相应节点
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null || k<=0) return null;
int count = 0;
ListNode current = head;
while(current != null) {
count++;
current = current.next;
}
if(count<k) return null;
current = head;
while(count-k!=0) {
current = current.next;
count--;
}
return current;
} //方法二:双链遍历
public ListNode FindKthToTail2(ListNode head, int k) {
if(head == null || k <= 0) return null;
ListNode infantry = head, res = head;
while(k>0) {
if(infantry == null) return null;
infantry = infantry.next;
k--;
}
while(infantry != null) {
infantry = infantry.next;
res = res.next;
}
return res;
} 无敌烦这种第几第几的,i 和 k 总是算不清楚。这边建议搞个伪头结点 从 0 开始会舒服一些
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
ListNode endK=head;
ListNode end=head;
ListNode dummy=new ListNode(0);
dummy.next=head;
end=dummy;
if(head==null){
return head;
}
int i=0;
while(end.next!=null&&i<k){
i++;
end=end.next;
}
if(i<k){
return null;
}
while(end.next!=null){
end=end.next;
endK=endK.next;
}
return endK;
}
}
/** * 利用栈 */ public ListNode FindKthToTail1(ListNode head, int k) { if (head == null) return null; Stack<ListNode> stack = new Stack<>(); while (head != null) { stack.push(head); head = head.next; } ListNode node = null; while (k > 0 && k <= stack.size()) { node = stack.pop(); k--; } return node; }
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode FindKthToTail(ListNode head,int k) { //方法1: // if(head==null||k<=0) // return null; // //1.遍历链表的长度 // int n=0; // ListNode tempNode = head; // while(tempNode!=null){ // n++; // tempNode = tempNode.next; // } // if(k>n) // return null; // //2.遍历获取第n-k+1个节点 // for(int i=0;i<n-k;i++){ // head = head.next; // } // return head; //方法2: /** * 思路: * 定义快指针和慢指针。 * 快指针先走 k-1 步,到达第 k 个节点。 * 然后两指针同时齐步走,当快指针到达末尾时,慢指针在倒数第 k 个节点上。 */ if (head == null || k <= 0) { return null; } ListNode slow = head; ListNode fast = head; for (int i = 1; i < k; i++) { if (fast.next == null) { return null; } fast = fast.next; } while (fast.next != null) { fast = fast.next; slow = slow.next; } return slow; } }
public class Solution {
int n = -1;
ListNode res = null;
public void FindKthToTailRecur(ListNode head, int k){
if (head == null){
n = 0;
return;
}
FindKthToTailRecur(head.next, k);
n++;
if (n == k){
res = head;
}
}
public ListNode FindKthToTail(ListNode head,int k) {
n = -1;
FindKthToTailRecur(head, k);
return res;
}
} /**
题目描述
输入一个链表,输出该链表中倒数第k个结点。
题解:
利用双指针
*/
public ListNode FindKthToTail(ListNode head,int k) {
ListNode first = head, end = head;
while (end != null) {
if (k > 0) {
end = end.next;
k--;
}else {
first = first.next;
end = end.next;
}
}
if (k > 0) {
return null;
}
return first;
} public class Solution {
public ListNode FindKthToTail(ListNode head, int k) {
if (head == null || k <= 0) {
return null;
}
ListNode slow = head;
ListNode fast = head;
for (int i = 0; i < k; i++) {
if (fast == null) {
return null;
}
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
}