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

[LeetCode]141 Linked List Cycle

时间:2015-01-09 01:52:33      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:leetcode

https://oj.leetcode.com/problems/linked-list-cycle/

http://blog.csdn.net/linhuanmars/article/details/21200601

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        
        // Use 2 pointers.
        
        if (head == null)
            return false;
            
        ListNode one = head;
        ListNode two = head;
        while (one != null && two != null)
        {
            one = one.next;
            two = two.next;
            
            if (two != null)
                two = two.next;
                
            if (one != null && one == two)
                return true;
        }
        return false;
    }
}


[LeetCode]141 Linked List Cycle

标签:leetcode

原文地址:http://7371901.blog.51cto.com/7361901/1600799

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