Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

合并两个有序链表 #289

Open
Sunny-117 opened this issue Nov 8, 2022 · 1 comment
Open

合并两个有序链表 #289

Sunny-117 opened this issue Nov 8, 2022 · 1 comment

Comments

@Sunny-117
Copy link
Owner

No description provided.

@lxy-Jason
Copy link
Contributor

/**
 * @param {ListNode} list1
 * @param {ListNode} list2
 * @return {ListNode}
 */
var mergeTwoLists = function (l1, l2) {
    if (!l1) {
        return l2;
    }
    else if (!l2) {
        return l1
    }
    else if(l1.val < l2.val){ //两个链表中头结点小的那个return
        l1.next = mergeTwoLists(l1.next,l2); // l1的头结点小于l2的头结点,递归比较l1.next和l2哪个小
        return l1;//最终返回的都是小的那个值,达到合并之后递增的目的
    }
    else{
        l2.next = mergeTwoLists(l1,l2.next);
        return l2;
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants