首页 > 其他 > 详细

【剑指offer】输出链表倒数第K个元素

时间:2018-11-28 11:04:45      阅读:140      评论:0      收藏:0      [点我收藏+]
 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(head == null) return null;
13         int length = 0;
14         ListNode tempNode = head;
15         ListNode result;
16         while(tempNode!=null){
17             length++;
18             tempNode = tempNode.next;
19         }
20         //判断K值是否合法
21         if(k > length) return null;
22         tempNode = head;
23         for(int i=0; i<length; i++){
24             if(i>=k){
25                 head = head.next;
26             }
27             tempNode  = tempNode.next;
28         }
29         return head;
30     }
31 }

 

【剑指offer】输出链表倒数第K个元素

原文:https://www.cnblogs.com/singular/p/10030032.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!