首页 > 其他 > 详细

14.链表倒数节点

时间:2019-04-06 17:18:53      阅读:130      评论:0      收藏:0      [点我收藏+]

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

注意不能直接head遍历,head返回,head是root;

/*
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;
        }
        int count =1;
        
        ListNode  old = head;
        while(head.next != null) {
            head = head.next;
            count++;
        }
        if(k>count) {
            return null;
        }
        for(int i= 0;i<count-k;i++) {
            old = old.next;
        }
        return old;
    }
}

 

14.链表倒数节点

原文:https://www.cnblogs.com/wzQingtTian/p/10662095.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!