AcWing 33. 链表中倒数第k个节点
原题链接
简单
作者:
小轩喵灬
,
2025-01-11 14:54:18
,
所有人可见
,
阅读 1
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode findKthToTail(ListNode pListHead, int k) {
if (pListHead == null) {
return null;
}
ListNode l1 = pListHead;
ListNode l2 = pListHead;
for (int i = 0; i < k; i++) {
if (l1 == null) {
return null;
}
l1 = l1.next;
}
while(l1 != null) {
l1 = l1.next;
l2 = l2.next;
}
return l2;
}
}