ํฐ์คํ ๋ฆฌ ๋ทฐ
Linked List
+ Merge Sort
Merge sort์ ๊ธฐ๋ณธ๊ฐ๋
์ Divide and conquer์ด๋ค.
๋ฐ๋ผ์ Quick sort์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฌ๊ท๋ฅผ ํตํด ๊ตฌํ ํ ์ ์๋ค.
์ด๋ฏธ ์ ๋ ฌ๋ ๋ ํํฐ์
๋ค์ด ์๋ค๋ฉด,
๋ ํํฐ์
์ ์ฒซ์์๋ฅผ ๊ฒ์ฌํ์ฌ ๋ ์์ ์์๋ฅผ ๋บ๋ค.
Linked List ์ ์ฐ๊ฒฐํ์ฌ์ ํธ๋ ๋ฌธ์ ์ด๊ธฐ๋๋ฌธ์
๋ ์์ ์์๋ฅผ ๊ฐ์ง๊ณ ์๋ ๋ง๋ฆฌ๋ฅผ ํค๋๋ก ์ผ๊ณ ,
ํค๋๋ก ์ผ์ ๋ง๋ฆฌ์ ๋ค์ ๋
ธ๋์, ํค๋๊ฐ ๋์ง๋ชปํ ๋ง๋ฆฌ ๋๊ฐ์ง๋ฅผ ๋ค์ ํจ์์ ๋ฃ๋๋ค.
21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new sorted list.
The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
const mergeTwoLists = function(l1, l2) {
if (!l1) return l2;
if (!l2) return l1;
if(l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
};
'๊ณต๋ถ > JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS/์๊ณ ๋ฆฌ์ฆ] Sort Colors : Dutch national flag problem (0) | 2020.09.07 |
---|---|
[JS/์๊ณ ๋ฆฌ์ฆ] Find First and Last Position of Element in Sorted Array (0) | 2020.09.07 |
[JS] Promise.all()์ ๋ณ๋ ฌ์ผ๊น ์ง๋ ฌ์ผ๊น๐ค (1) | 2020.08.22 |
[JS] Prevent Stack Overflow (0) | 2020.08.06 |
[JS] #๋ชจ์๊ณ ์ฌ - ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ์ฐ์ต (0) | 2020.07.12 |
๋๊ธ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ์์ฑ์ํจ์
- array
- HTML
- ๋ฐ๋๋ผ์ฝ๋ฉ ํ๊ธฐ
- VSC
- ๋ฐ๋๋ผ์ฝ๋ฉ
- eslint
- ์ฝ๋ฉ๋ถํธ์บ ํ
- book
- stackoverflow
- DOM
- js
- string
- css
- Stash
- eventlistener
- ๋ถํธ์บ ํ
- KEYBOARD
- GIT
- review
- Total
- Today
- Yesterday