160. Intersection of Two Linked Lists

김현민·2021년 12월 15일
0

Algorithm

목록 보기
120/126
post-thumbnail

코드

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function(headA, headB)  
{
     if (!headA || !headB) return null;  
    
    let curA = headA
    let curB = headB
    
    while(curA!== curB){
        
        curA = curA ? curA.next : headB
        curB = curB ? curB.next : headA
    }
    
    return curA
    
};

headA를 기준으로 잡고 같은게 있으면 그 노드에 포인터를 가리키게 한다.

profile
Jr. FE Dev

0개의 댓글