标签:
输入一个链表,输出该链表中倒数第k个结点。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if (head == null || k == 0) {
return null;
}
ListNode right = head;
for (int i = 0; i < k -1; i++) {
if (right.next != null) {
right = right.next;
} else {
return null;
}
}
ListNode left = head;
while (right.next != null) {
left = left.next;
right = right.next;
}
return left;
}
}
标签:
原文地址:http://www.cnblogs.com/rosending/p/5617984.html