CTCI 2.6

Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.
DEFINITION
Circular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input:A ->B->C->D->E-> C[thesameCasearlier] 
Output:C

There is a same problem on leetcode. Use a fast pointer and a slow pointer to find the meet point, then use two slow pointer from the head of the list and the meet point, the position where this two pointers meet is just the start point. Another way is to use HashSet to record each node, the first node exist in the set would be the start point.

/*We could use a fast pointer and a slow pointer to find the node where the two pointer meets, since the 
list is circlelist, we are sure to find such a node. We could have:
Fast_length = Slow_length + n * circle_length, where n >= 1, since fast length must catch up slow one before 
it finishes one circle. We define the distance of list head to where circle start as K, and the start to where
two pointers meet as L, then it is easy to show: 
Slow_length = K + L, we restrict the speed of fast as two times of the slow, 2 nodes for fast and 
one node for slow,  then we could have
Slow_length = n * circle_length, then we have
K + L = n * circle_length, rewrite it as
K  = (n-1)*circle_length + (circle_length - L). From this, we know that from the meet point to the start point,
it is just the disctance from head to start point, plus some circle_length. Then we could have two slow
pointer start from head and meet point, where they meet is just the start point of the circle. The time complexity
is O(N) and the space complexity is O(1).

Another way is to use HasSet to record each node, the first node that exist in hashset would be the start
point. The time complexity is O(N) and space complexity is O(N)
*/

import java.util.*;

public class CircleListStartPoint {
    public Node findStart(Node head) {
        //Pay attention to the first case
        Node slow = head, fast = head;
        do {
            slow = slow.next;
            fast = fast.next.next;
        }while(slow != fast);
        Node temp = head;
        while(slow != temp) {
            slow = slow.next;
            temp = temp.next;
        }
        return slow;
    }

    public Node findStart2(Node head) {
        HashSet<Node> set = new HashSet<Node>();
        Node temp = head;
        while(true) {
            if(set.contains(temp) == false) {
                set.add(temp); temp = temp.next;
            }
            else {
                return temp;
            }
        }
    }

    public static void main(String[] args) {
        Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); 
        Node node4 = new Node(4); Node node5 = new Node(5); Node node6 = new Node(6);
        node1.next = node2; node2.next = node3; node3.next = node4; node4.next = node5;
        node5.next = node6; node6.next = node1;
        CircleListStartPoint clsp = new CircleListStartPoint();
        System.out.println(clsp.findStart(node1).val); 
    }
}

 

转载于:https://www.cnblogs.com/pyemma/p/3832317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值