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

3 - Two Pointers Algorithm

时间:2019-05-18 14:01:21      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:pointer   href   strong   fas   sts   algo   null   www.   div   

380. Intersection of Two Linked Lists

https://www.lintcode.com/problem/intersection-of-two-linked-lists/description?_from=ladder&&fromId=1

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        // write your code here
        if(headA == null || headB == null) {
            return null;
        }
        ListNode a = headA;
        ListNode b = headB;
        while(a != b) {
            a = a == null ? headB : a.next;
            b = b == null ? headA : b.next;
        }
        return a;
    }

 

102. Linked List Cycle

https://www.lintcode.com/problem/linked-list-cycle/description?_from=ladder&&fromId=1

    public boolean hasCycle(ListNode head) {
        // write your code here
        if(head == null || head.next == null) {
            return false;
        }
        ListNode slow = head, fast = head.next;
        while(slow != fast) {
            if(fast == null || fast.next == null) {
                return false;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return true;
    }

 

3 - Two Pointers Algorithm

标签:pointer   href   strong   fas   sts   algo   null   www.   div   

原文地址:https://www.cnblogs.com/jenna/p/10885347.html

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