3 - Two Pointers Algorithm

Posted jenna

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3 - Two Pointers Algorithm相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

3 - Two Pointers Algorithm

3 - Two Pointers Algorithm

3 - Two Pointers Algorithm

3 - Two Pointers Algorithm

sorting, two pointers(cf div.3 1113)

[LeetCode-Java] Two Pointers (1-300)