[Leetcode] 21. Merge Two Sorted Lists

Seongjun Lee·2022년 2월 19일
0

[Leetcode] - Problem

목록 보기
21/43
post-thumbnail

Problem

문제 링크

두 숫자로 이뤄진 링크드리스트를 오름차순으로 머지하라.

Solution

  1. list1과 list2 중에서 값이 작은 노드를 head 노드가 가르키게한다.
  2. 작은 노드로 선정된 노드는 다음노드를 가르키도록한다.
  3. 그렇게 두 list 노드가 null을 가르킬때까지 head에 이어붙어나간다.

JS CODE

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} list1
 * @param {ListNode} list2
 * @return {ListNode}
 */
var mergeTwoLists = function (list1, list2) {
  const head = new ListNode()

  let node = {
    l1: list1,
    l2: list2,
    rear: head,
  }

  const getValMvNode = (node, keyStr) => {
    const { val, next } = node[keyStr]
    node[keyStr] = next
    return val
  }

  while (node.l1 || node.l2) {
    let curNodeVal = null
    if (node.l1 && node.l2) {
      curNodeVal = node.l1.val < node.l2.val ? getValMvNode(node, 'l1') : getValMvNode(node, 'l2')
    } else if (node.l1) curNodeVal = getValMvNode(node, 'l1')
    else if (node.l2) curNodeVal = getValMvNode(node, 'l2')

    // console.log(l1, l2)

    node.rear.next = new ListNode(curNodeVal)
    node.rear = node.rear.next
  }

  return head.next
}
profile
Hi there 👋

0개의 댓글