2009计算机考研题:查找链表中倒数第k个结点



原理:两个指针先都指向头指针的下一节点,一个指针先走K-1步,然后俩指针再一起走,后走的指针所指为所求,注意边界处理。

Java代码 复制代码 收藏代码
  1. class Node{
  2. Node link;
  3. int data;
  4. public Node(){}
  5. public Node(int m) {
  6. data = m;
  7. }
  8. }
  9. public class MyLinkList {
  10. public Node createLinkList(int[] a){
  11. Node head_node = new Node();
  12. Node temp_node = head_node;
  13. for(int m : a){
  14. Node n = new Node(m);
  15. temp_node.link = n;
  16. temp_node = n;
  17. }
  18. return head_node;
  19. }
  20. public int printK_NodeOfCountFromEnd(Node head_node,int k){
  21. if(null == head_node || k <= 0)
  22. return 0;
  23. Node temp1_node = head_node.link;
  24. for(int i = 0; i < k-1 && null != temp1_node; i++)
  25. temp1_node = temp1_node.link;
  26. if(null == temp1_node)
  27. return 0;
  28. Node temp2_node = head_node.link;
  29. while(null != temp1_node.link){
  30. temp1_node = temp1_node.link;
  31. temp2_node = temp2_node.link;
  32. }
  33. System.out.println(temp2_node.data);
  34. return 1;
  35. }
  36. public static void main(String[] args){
  37. MyLinkList list = new MyLinkList();
  38. int[] a = {1,4,2,5,3};
  39. list.printK_NodeOfCountFromEnd(list.createLinkList(a), 2);
  40. }
  41. }
    class Node{  
        Node link;  
        int data;  
          
        public Node(){}  
        public Node(int m) {  
            data = m;  
        }  
          
    }  
    public class MyLinkList {  
          
        public Node createLinkList(int[] a){  
              
            Node head_node = new Node();  
            Node temp_node = head_node;  
            for(int m : a){  
                Node n = new Node(m);  
                temp_node.link = n;  
                temp_node = n;  
            }  
            return head_node;  
        }  
          
        public int printK_NodeOfCountFromEnd(Node head_node,int k){  
              
            if(null == head_node || k <= 0)  
                return 0;  
            Node temp1_node = head_node.link;  
            for(int i = 0; i < k-1 && null != temp1_node; i++)  
                temp1_node = temp1_node.link;  
            if(null == temp1_node)  
                return 0;  
            Node temp2_node = head_node.link;  
            while(null != temp1_node.link){  
                temp1_node = temp1_node.link;  
                temp2_node = temp2_node.link;  
            }  
            System.out.println(temp2_node.data);  
            return 1;  
        }  
        public static void main(String[] args){  
            MyLinkList list = new MyLinkList();  
            int[] a = {1,4,2,5,3};  
            list.printK_NodeOfCountFromEnd(list.createLinkList(a), 2);  
        }  
          
    }  


运行:
C:\test>java MyLinkList
5

文章转自:[url]http://blog.csdn.net/nash_/article/details/8205491
[/url]
源码: 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值