首页 > 学院 > 开发设计 > 正文

剑指offer经典编程(九)

2019-11-08 19:24:26
字体:
来源:转载
供稿:网友

链表中倒数第k个结点

输入一个链表,输出该链表中倒数第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 pHead = head; ListNode pBehind = null; for (int i =0;i<k-1;i++){ if(pHead.next != null){ pHead=pHead.next; }else { return null; } } pBehind = head; while (pHead.next!=null){ pHead =pHead.next; pBehind = pBehind.next; } return pBehind; }

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表