반응형
728x90
반응형
문제
https://leetcode.com/problems/merge-two-sorted-lists/
풀이코드
package SITE03_leetcode.easy;
import SITE03_leetcode.common.ListNode;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* https://leetcode.com/problems/merge-two-sorted-lists/
*/
public class E002_leetCode21_MergeTwoSortedLists {
public static void main(String[] args) {
E002_leetCode21_MergeTwoSortedLists solution = new E002_leetCode21_MergeTwoSortedLists();
ListNode listNode1 = new ListNode();
listNode1.val = 1;
listNode1.next = new ListNode();
listNode1.next.val = 2;
listNode1.next.next = new ListNode();
listNode1.next.next.val = 4;
ListNode listNode2 = new ListNode();
listNode2.val = 1;
listNode2.next = new ListNode();
listNode2.next.val = 3;
listNode2.next.next = new ListNode();
listNode2.next.next.val = 4;
System.out.println(solution.mergeTwoLists(listNode1, listNode2));
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode temp = new ListNode(0);
ListNode head = temp;
while (true) {
/* null check */
if (l1 == null) {
temp.next = l2;
break;
} else if (l2 == null) {
temp.next = l1;
break;
}
// 더 작은 노드를 설정
if (l1.val <= l2.val) {
temp.next = l1;
l1 = l1.next;
} else {
temp.next = l2;
l2 = l2.next;
}
temp = temp.next;
}
return head.next;
}
}
반응형
'Algorithm > Problem Solving' 카테고리의 다른 글
[leetcode] Easy-34번. Find First and Last Position of Element in Sorted Array 문제풀이 (0) | 2021.11.23 |
---|---|
[leetcode] Easy-31번. Next Permutation 문제풀이 (0) | 2021.11.20 |
[leetcode] Medium-47번. Permutations2 문제풀이 (0) | 2021.11.17 |
[leetcode] Medium-46번. Permutations 문제풀이 (0) | 2021.11.13 |
[leetcode] Medium-39번. Combination Sum 문제풀이 (0) | 2021.11.12 |