// 题目描述 // 输入一个链表,输出该链表中倒数第k个结点。 public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } } public static ListNode FindKthToTail(ListNode head,int k) { Stack<ListNode> listNodeStack = new Stack<ListNode>(); while(head!=null){ listNodeStack.push(head); head=head.next; } while (!listNodeStack.isEmpty()){ if (k==1){ return listNodeStack.pop(); } k--; listNodeStack.pop(); } return null; }
原文地址:https://www.cnblogs.com/kaibing/p/8997523.html
时间: 2024-11-08 23:41:14