设为首页 加入收藏

TOP

Leetcode - linked list circle 2
2015-07-20 17:32:00 来源: 作者: 【 】 浏览:2
Tags:Leetcode linked list circle

貌似国内面试经常问得题目,以前没遇到过。。

思路:

1: 两个指针,一个一次走两格,一个一次走一个。相遇了证明有环。

2: 然后因为快的那个总是比慢的那个走快一步,当他们相遇后,再走k步(k是circle的结点数量)后,他们会再相遇,从而求出circle结点数量。

3: 最后再设置两个指针(都是一次走一步),但一开始一个比另外一个领先k步,那么它们就会刚好在环的起点相遇。。


但是偶在初始化那里接连犯了好几个错误。


该题目的另外一个变形:“两个链表,求相交的第一个节点”。



import java.util.*;

  class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
         val = x;
         next = null;
    }
  }

public class Solution {
	
	ListNode dCycle(ListNode head)
	{
		if(head == null || head.next == null)
			return null;
		ListNode pSlow,pFast;
		pSlow = head;
		pFast = head.next;
		
		while(pFast.next != null && pFast.next.next != null && pFast != pSlow)
		{
			pFast = pFast.next.next;
			pSlow = pSlow.next;
		}
		
		if(pFast.next == null || pFast.next.next == null)
			return null;
		return pFast;
	}
	
	int CycleN(ListNode cnode)
	{
		ListNode pFast,pSlow;
		pSlow = cnode.next;
		pFast = cnode.next.next;
		
		int k = 1;
		while(pFast != pSlow)
		{
			pFast = pFast.next.next;
			pSlow = pSlow.next;
			k++;
		}
		
		return k;
	}
	
    public ListNode detectCycle(ListNode head) {
    	
    	if(head == null) return null;
    	else if(head.next == null) return null;
    	
        ListNode cNode = dCycle(head);
        
        if(cNode == null) return null;
        
        int iCycleN = CycleN(cNode);
        
        ListNode pAdvNode = head;
        ListNode pNode = head;
        
        for(int i=0;i
  
   

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ3187 Backward Digit Sums 下一篇Effective C++ 29-33

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·在 Redis 中如何查看 (2025-12-26 03:19:03)
·Redis在实际应用中, (2025-12-26 03:19:01)
·Redis配置中`require (2025-12-26 03:18:58)
·Asus Armoury Crate (2025-12-26 02:52:33)
·WindowsFX (LinuxFX) (2025-12-26 02:52:30)