剑指offer之链表中倒数第K个结点(Java实现)

版权声明:转载请联系 :[email protected] https://blog.csdn.net/weixin_40928253/article/details/85609829

链表中倒数第K个结点

NowCoder

题目描述:

输入一个链表,输出该链表中倒数第k个结点。

解题思路:

//时间复杂度O(n),一次遍历即可
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        ListNode pre=null,p=null;
        //两个指针都指向头结点
        p=head;
        pre=head;
        //记录k值
        int a=k;
        //记录节点的个数
        int count=0;
        //p指针先跑,并且记录节点数,当p指针跑了k-1个节点后,pre指针开始跑,
        //当p指针跑到最后时,pre所指指针就是倒数第k个节点
        while(p!=null){
            p=p.next;
            count++;
            if(k<1){
                pre=pre.next;
            }
            k--;
        }
        //如果节点个数小于所求的倒数第k个节点,则返回空
        if(count<a) return null;
        return pre;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40928253/article/details/85609829