码迷,mamicode.com
首页 > 其他好文 > 详细

链表判环问题

时间:2017-09-22 00:47:12      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:val   并且   问题   handle   next   tle   value   one   ring   

给定一单链表的头指针head

1、判断链表是否又环。

2、如果有环,求环长以及环的起始节点。

 

package aglist;

class Node{
    public final int value;
    public Node next = null;
    Node(int value){
        this.value = value;
    }
    public static void addTail(Node head,Node node){
        Node temp = head;
        while(temp.next != null){
            temp = temp.next;
        }
        temp.next = node;
    }
    public static Node getNode(Node head,int index){
        int i = 0;
        Node temp = head;
        while(i < index && temp.next != null){
            temp = temp.next;
            i++;
        }
        return temp;
    }
    public static Node getTail(Node head){
        Node temp = head;
        while(temp.next != null){
            temp = temp.next;
        }
        return temp;
    }
}
public class Circle {
    public static boolean hasCircle(Node head){
        Node fast = head,slow = head;//两指针,一个步长为1,一个步长为2
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(fast == slow){return true;}//两指针相遇,必有环,且相遇点在环内
        }
        return false;
    }
    private static Node getMeetNode(Node head){
        Node fast = head,slow = head;
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(fast == slow){break;}
        }
        return fast;
    }
    private static int getLengthOfCircle(Node meetNode){
        int length = 1;
        Node temp = meetNode.next;
        while(temp != meetNode){//由于相遇点必在环内,只要顺着环走一圈,即可获得环的长度
            length++;
            temp = temp.next;
        }
        return length;
    }
    /*
     * 在相遇点将环分开,视为形成两个单链表
     * 一个单链表以head为头
     * 另一个单链表以meetNode为头
     * 该问题进而规约为求两个单链表的交点,交点即为环的起点
     * 并且其交点到两链表头等长
     * */
    private static Node getStartOfCircle(Node head,Node meetNode){
        while(head != meetNode){
            head = head.next;
            meetNode = meetNode.next;
        }
        return meetNode;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            int[] ary = {3,4,5,7,1,40,10,50};
            Node head = new Node(30);
            for (int i = 0; i < ary.length; i++) {
                Node.addTail(head, new Node(ary[i]));
            }
            Node.getTail(head).next = Node.getNode(head, 0);
            if (hasCircle(head)) {
                Node meetNode = getMeetNode(head);
                int lenght = getLengthOfCircle(meetNode);
                Node start = getStartOfCircle(head, meetNode);
                System.out.println("length:"+lenght);
                System.out.println("StartNode:"+start.value);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

}

 

package aglist;
class Node{public final int value;public Node next = null;Node(int value){this.value = value;}public static void addTail(Node head,Node node){Node temp = head;while(temp.next != null){temp = temp.next;}temp.next = node;}public static Node getNode(Node head,int index){int i = 0;Node temp = head;while(i < index && temp.next != null){temp = temp.next;i++;}return temp;}public static Node getTail(Node head){Node temp = head;while(temp.next != null){temp = temp.next;}return temp;}}public class Circle {public static boolean hasCircle(Node head){Node fast = head,slow = head;//两指针,一个步长为1,一个步长为2while(fast.next != null && fast.next.next != null){slow = slow.next;fast = fast.next.next;if(fast == slow){return true;}//两指针相遇,必有环,且相遇点在环内}return false;}private static Node getMeetNode(Node head){Node fast = head,slow = head;while(fast.next != null && fast.next.next != null){slow = slow.next;fast = fast.next.next;if(fast == slow){break;}}return fast;}private static int getLengthOfCircle(Node meetNode){int length = 1;Node temp = meetNode.next;while(temp != meetNode){//由于相遇点必在环内,只要顺着环走一圈,即可获得环的长度length++;temp = temp.next;}return length;}/* * 在相遇点将环分开,视为形成两个单链表 * 一个单链表以head为头 * 另一个单链表以meetNode为头 * 该问题进而规约为求两个单链表的交点,交点即为环的起点 * 并且其交点到两链表头等长 * */private static Node getStartOfCircle(Node head,Node meetNode){while(head != meetNode){head = head.next;meetNode = meetNode.next;}return meetNode;}public static void main(String[] args) {// TODO Auto-generated method stubtry {int[] ary = {3,4,5,7,1,40,10,50};Node head = new Node(30);for (int i = 0; i < ary.length; i++) {Node.addTail(head, new Node(ary[i]));}Node.getTail(head).next = Node.getNode(head, 0);if (hasCircle(head)) {Node meetNode = getMeetNode(head);int lenght = getLengthOfCircle(meetNode);Node start = getStartOfCircle(head, meetNode);System.out.println("length:"+lenght);System.out.println("StartNode:"+start.value);}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}
}

 

链表判环问题

标签:val   并且   问题   handle   next   tle   value   one   ring   

原文地址:http://www.cnblogs.com/qcblog/p/7572090.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!