-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
链表相关
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |