题目
https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=190&&tqId=35368&rp=1&ru=/ta/job-code-high-rd&qru=/ta/job-code-high-rd/question-ranking
/*
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;
}
}
全部评论
(0) 回帖