Skip to content

Commit

Permalink
Create ListNodeProblem.java
Browse files Browse the repository at this point in the history
链表相关
  • Loading branch information
JinhouLiu authored Jul 28, 2024
1 parent 11d770a commit 1904871
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions ListNodeProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead ListNode类
* @param k int整型
* @return ListNode类
*/
public ListNode FindKthToTail (ListNode pHead, int k) {
if (pHead == null||k==0) {
return null;
}

Stack<ListNode> stack = new Stack<>();

ListNode current = pHead;
while (current != null) {
stack.add(current);
current = current.next;
}
if (stack.size() < k) {
return null;
}
int curIndex = 1;
while (stack.size() != 0 && curIndex < k) {
stack.pop();
curIndex++;
}
return stack.pop();
}

0 comments on commit 1904871

Please sign in to comment.