Skip to content

LeetCode - 21. Merge Two Sorted Lists javascript #1

@vitabyfrank

Description

@vitabyfrank

Recursion + Linked List

  • 문제를 제대로 이해하지 못함.
  • 재귀함수 개념 + 링드 리스트 개념 확인하기.
let mergeTwoLists = function(l1, l2) {
    let fh = new ListNode(-1);
    let current = fh;
    while(l1 || l2){

        if(l2 == null || (l1 !== null && l1.val <= l2.val)){
            fh.next = l1;
            fh = fh.next;
            l1 = l1.next;
        } else {
            fh.next = l2;
            fh = fh.next;
            l2 = l2.next;
        }
    }
    return current.next;
};
var mergeTwoLists = function (l1, l2) {
  if (!l1 || !l2) return l1 || l2;
  if (l1.val < l2.val) {
    l1.next = mergeTwoLists(l1.next, l2);
    return l1;
  }
  l2.next = mergeTwoLists(l1, l2.next);
  return l2;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    wrongdouble check

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions