
문제링크
Merge Two Sorted Lists - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
생각정리

기억하면 좋을 것
1. 두 list를 비교 후 남은 node 처리 할 때
crt.next = list1 || list2
이렇게 깔끔하게 처리 해줄 수 있다.
2. crt.next 를 지정 해 준 후 crt = crt.next 로 다음 노드로 이동 해 주어야 한다.
소감
이 문제는 혼자서 해결이 안 돼서 다른 사람의 코드를 보고 공부하며 풀었다.
오늘이 퇴근 후 알고리즘을 공부 한 지 3일째 되는 날이다
어찌됐든 작심3일은 한 거다.
조금씩 지식을 쌓아가자 !
해결 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mergeTwoLists = function (list1, list2) { | |
let mergeHead = { val:-1,next: null}; | |
let crt = mergeHead; | |
while(list1 && list2){ | |
if(list1.val < list2.val){ | |
crt.next = list1; | |
list1 = list1.next; | |
}else{ | |
console.log(crt); | |
crt.next = list2; | |
list2 = list2.next; | |
} | |
crt = crt.next; | |
} | |
crt.next = list1 || list2; | |
return mergeHead.next; | |
}; |
'알고리즘 > leetcode' 카테고리의 다른 글
[Leetcode/JS] 404. Sum of Left Leaves (0) | 2023.07.17 |
---|---|
[Leetcode/JS] 111. Minimum Depth of Binary Tree (0) | 2023.07.13 |
[leetcode/JS] 234. Palindrome Linked List / Javascript (0) | 2022.08.25 |
[leetcode/JS] 70. Climbing Stairs / Javascript (0) | 2022.08.23 |
[leetcode/JS] 617. Merge Two BInary Trees / Javascript (0) | 2022.05.12 |
댓글