(十四)(需要好好理解)
题目描述:输入一个链表,输出该链表中倒数第k个结点。
1 /* 2 public class ListNode { 3 int val; 4 ListNode next = null; 5 6 ListNode(int val) { 7 this.val = val; 8 } 9 }*/ 10 public class Solution { 11 public ListNode FindKthToTail(ListNode head,int k) { 12 if(k<=0) return null; 13 ListNode p1 = head; 14 ListNode p2 = head; 15 for(int i=0;i<k-1;i++){ 16 if(p2==null) 17 return null; 18 p2 = p2.next; 19 } 20 if(p2==null) return null; 21 while(p2.next!=null){ 22 p1 = p1.next; 23 p2 = p2.next; 24 } 25 return p1; 26 } 27 }