15、链表中倒数第k个节点

来源:互联网 发布:剑三苍云捏脸数据成女 编辑:程序博客网 时间:2024/06/05 05:13
/*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 head;        ListNode h = new ListNode(0);        h.next = head;        ListNode slow = h, fast = h;        int count = k;        while(k-- > 0 && fast.next != null){fast = fast.next;        }        if(k != -1) return null;        while(fast.next != null){slow = slow.next;            fast = fast.next;        }        return slow.next;    }}

原创粉丝点击