作者:含糊hagle | 来源:互联网 | 2023-10-10 08:25
题目描述输入一个链表,输出该链表中倒数第k个结点。*publicclassListNode{intval;ListNodenextnull;}}ListNod
题目描述
输入一个链表,输出该链表中倒数第k个结点。
/*
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|| k==0){return null;}ListNode tip=head;for(int i=1;iif(head.next!=null){head=head.next; }else{return null;}}while(head.next!=null){head=head.next;tip=tip.next;}return tip;
}