## Topic description:

Input a linked list and output the kth node from the bottom of the linked list.

Problem solving plan:

Let the linked list start from the beginning to the end, and reduce the value of k by 1 for each step you move.

①Assuming that the linked list is 1-2-3, k=4, there is no fourth-to-last node in the linked list at all.

The nodes reached are: 1-2-3

The change of k is: 3 2 1

②Assuming that the linked list is 1-2-3, k=3, and the third node from the bottom of the linked list is 3

The nodes reached are: 1-2-3

The change of k is: 2 1 0

③ Assume that the linked list is 1-2-3, k=2, and the penultimate node of the linked list is 2

The nodes reached are: 1-2-3

The change of k is: 1 0 -1

In the above three cases, if the value of k is greater than 0, it means that the k-th node from the bottom cannot be found, and the value of k is larger than the length of the linked list; if the value of k is equal to 0, it means that the head node of the linked list is the k-th node from the bottom; if the value of k is the k-th node from the bottom Less than 0, how to deal with this situation?

First, after walking from the beginning to the end, the k value is reduced by 1 for each move, and the k value becomes kN (N is the length of the linked list), then let the current k value go through the head node again, and increase the k value by 1 for each move. , when the value of k is 0, it is the Nk node, which is the previous node of the last k-th node to be found.

The core code is implemented as follows:

```public ListNode FindKthToTail(ListNode head,int k) {
if(head == null || k < 1){
System.out.println("The linked list is empty or the value of k is less than 1!");
return null;
}
while(cur != null){
k--;
cur = cur.next;
}
if(k > 0){
System.out.println("The value of k is greater than the length of the linked list!");
return null;
}else if(k == 0){
}else{
while(++k != 0){
cur = cur.next;
}
}
return cur.next;
}```
Test code:
```class LinkList{
private int length;

class ListNode {
int val;
ListNode next = null;

ListNode(int val) {
this.val = val;
}
}

length = 0;
}

// Append the node to the end of the linked list
public void appendLast(int value) {
ListNode node = new ListNode(value);
length++;
return;
}
while(cur.next != null){
cur = cur.next;
}
cur.next = node;
length++;
}

public void printList(){
while(cur != null){
System.out.print(cur.val + " ");
cur = cur.next;
}
}

public ListNode FindKthToTail(ListNode head,int k) {
if(head == null || k < 1){
System.out.println("The linked list is empty or the value of k is less than 1!");
return null;
}
while(cur != null){
k--;
cur = cur.next;
}
if(k > 0){
System.out.println("The value of k is greater than the length of the linked list!");
return null;
}else if(k == 0){
}else{
while(++k != 0){
cur = cur.next;
}
}
return cur.next;
}
}

public class FindKthToTail {

public static void main(String[] args) {
list.appendLast(1);
list.appendLast(2);
list.appendLast(3);
list.appendLast(4);
list.appendLast(5);
list.appendLast(6);
list.appendLast(7);
list.appendLast(8);

list.printList();
System.out.println();

if(ret == null){
System.out.println("There is an error in the linked list or the K value is set incorrectly!");
}else{
System.out.println(ret.val);
}
}
}```
result:

### Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325496253&siteId=291194637
Recommended
Ranking
Daily