每天刷一道牛客题霸-第18天-链表中倒数第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) { return null; } if(k==1&&head.next==null) { return head; } int max=1; ListNode node=head; ListNode nodeHead=head; while((node=node.next)!=null) { max++; } node=head; if(max<k) { return null; } for(int i = 1 ;i <=max-k;i++) { nodeHead=nodeHead.next; node=nodeHead; } return node; } }#牛客题霸##题解#