zoukankan      html  css  js  c++  java
  • 剑指offer14题

    class ListNode {
        int val;
        ListNode next = null;
    
        ListNode(int val) {
            this.val = val;
        }
    }
    
    /**
     * 输入一个链表,输出该链表中倒数第k个结点。
     * 注意:编程时,首先判断非法条件
     */
    public class Solution14 {
        public ListNode FindKthToTail(ListNode head, int k) {
            if (head == null) {
                return head;
            }
    
            ArrayList<ListNode> listNodes = new ArrayList<>();
            int len = 0;
            while (head != null) {
                listNodes.add(head);
                len++;
                head = head.next;
            }
            return k > len ? null : listNodes.get(len - k);
        }
    }
  • 相关阅读:
    非空约束
    leetcode208
    leetcode207
    leetcode395
    leetcode116
    leetcode105
    leetcode131
    leetcode73
    leetcode200
    leetcode17
  • 原文地址:https://www.cnblogs.com/Adam-Ye/p/13458170.html
Copyright © 2011-2022 走看看