输出链表的倒数第k个节点

public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head==null||k<0){ //条件不符合直接返回空
            return null;
        }
        ListNode cur=head;
        int length=0;//求长度
        for(cur=head;cur!=null;cur=cur.next)
        {
            length++;
        }
        if (k > length)
        {
           return null;
         }
        cur=head;
        int len=length-(k-1);
        for(int i=1;i<len;i++)//遍历输出
        {
            cur=cur.next;
        }
        return cur;	
    }
}
发布了48 篇原创文章 · 获赞 5 · 访问量 2162

猜你喜欢

转载自blog.csdn.net/chris__x/article/details/104596524