https://leetcode.com/problems/intersection-of-two-linked-lists/

 

Intersection of Two Linked Lists - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

두 ListNode가 교차하는 첫번째 ListNode를 구하는 문제이다.

ListNode를 거꾸로 해서 뒤에서부터 같은지점까지를 반환하게끔 고민했으나 

순번대로 하는게 더 쉬웠음을.. 깨닫는다.

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;
            
        ListNode pa = headA, pb = headB;
        while(pa != pb) {
            pa = pa == null ? headB : pa.next;
            pb = pb == null ? headA : pb.next;
        }
        return pa;
    }

참고 코드

'Algorithm > LeetCode' 카테고리의 다른 글

LeetCode 763. Partition Labels  (0) 2022.07.04
LeetCode 22. Generate Parentheses  (0) 2022.06.30
LeetCode 21. Merge Two Sorted Lists  (0) 2022.06.29
LeetCode 234. Palindrome Linked List  (0) 2022.06.28
LeetCode 206. Reverse Linked List  (0) 2022.06.28

+ Recent posts