码迷,mamicode.com
首页 > 其他好文 > 详细

剑指offer-链表中倒数第 K 个结点

时间:2019-02-13 22:35:08      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:col   链表   输出   turn   desc   scribe   tno   next   bsp   

输入一个链表,输出该链表中倒数第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;
        }
        ListNode P1 = head;
        while(P1 != null && k-- > 0) {
            P1 = P1.next;
        }
        if(k > 0) return null;
        ListNode P2 = head;
        while(P1 != null) {
            P1 = P1.next;
            P2 = P2.next;
        }
        return P2;
    }
}

 

剑指offer-链表中倒数第 K 个结点

标签:col   链表   输出   turn   desc   scribe   tno   next   bsp   

原文地址:https://www.cnblogs.com/Roni-i/p/10371960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!