MCPcopy Create free account
hub / github.com/ROUTINE-STUDY/Algorithm / Sanghoo

Class Sanghoo

LeetCode/LinkedList/21. Merge Two Sorted Lists/Sanghoo.java:9–48  ·  view source on GitHub ↗

https://leetcode.com/problems/merge-two-sorted-lists/

Source from the content-addressed store, hash-verified

7 * https://leetcode.com/problems/merge-two-sorted-lists/
8 */
9public class Sanghoo {
10
11 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
12 LinkedList<ListNode> linkedList = new LinkedList<>();
13
14 // LinkedList에 각각의 Node를 담기
15 while(l1 != null) {
16 linkedList.add(l1);
17 l1 = l1.next;
18 }
19
20 while(l2 != null) {
21 linkedList.add(l2);
22 l2 = l2.next;
23 }
24
25 // 값을 기준으로 정렬
26 linkedList.sort(new Comparator<ListNode>() {
27 @Override
28 public int compare(ListNode o1, ListNode o2) {
29 if(o1.val > o2.val) return 1;
30 else if(o1.val < o2.val) return -1;
31 return 0;
32 }
33 });
34
35 // return 객체 생성
36 ListNode tempNode = new ListNode();
37 ListNode res = tempNode;
38
39 for(ListNode node : linkedList) {
40 tempNode.next = node;
41 tempNode = tempNode.next;
42 }
43
44 return res.next;
45 }
46
47
48}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected