单向链表反转,二分查找法

    
    public static void main(String[] args){
        Node node = new Node();
        node.value = 1;
        
        Node node2 = new Node();
        node2.value = 2;
        
        Node node3 = new Node();
        node3.value = 3;
        
        
        Node node4 = new Node();
        node4.value = 4;
        
        node.next=node2;
        node2.next=node3;
        node3.next = node4;
        
        
        Node previous = null;                // 前一个
        Node next = node;                    // 后一个
        
        //单向链表反转
        while((node = next) != null){
            next = node.next;
            node.next = previous;    //  下一个指向自己
            previous = node;
        }
        System.out.println(previous);
        
        int[] is ={1,2,3,4,5,6,7,8,9,10};
        
        //二分查找法
        int low = 0;
        int hight =  9;
        int key = 10;
        while(low <= hight){
            int mid = (low + hight) / 2;
            System.out.println(low + ":" + hight);
            if(is[mid] > key){
                hight = mid - 1;
            }else if(is[mid] < key){
                low = mid + 1;
            }else{
                System.out.println(mid);
                return;
            }
        }
        
    }
    
    static class Node{
        int value;
        Node next;
        
        public String toString(){
            String value = this.value+",";
            Node node = this;
            while((node = node.next) != null){
                value += node.value+",";
            }
            return value;
        }
    }

 

posted @ 2014-05-04 18:23  JimmyYong  阅读(820)  评论(0编辑  收藏  举报