首页 > 其他 > 详细

[LeetCode]141 Linked List Cycle

时间:2015-01-09 01:50:55      阅读:320      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!