-
21. Merge Two Sorted Lists javaLeetCode_Study_Plan/LeetCode 75 2022. 10. 5. 23:12
https://leetcode.com/problems/merge-two-sorted-lists/?envType=study-plan&id=level-1
class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { ListNode sortedNode = new ListNode(); ListNode curr_head = sortedNode; if (list1 == null) curr_head.next = list2; if (list2 == null) curr_head.next = list1; while(list1 != null && list2 != null){ if (list1.val < list2.val) { curr_head.next = list1; list1 = list1.next; } else{ curr_head.next = list2; list2= list2.next; } curr_head = curr_head.next; System.out.println(list1); if (list1 == null) curr_head.next = list2; if (list2 == null) curr_head.next = list1; } return sortedNode.next; } }
'LeetCode_Study_Plan > LeetCode 75' 카테고리의 다른 글
876. Middle of the Linked List java (0) 2022.10.05 206. Reverse Linked List java (0) 2022.10.05 392. Is Subsequence java (0) 2022.10.04 [LeetCode 75] 중간 점검 (0) 2022.10.02 205. Isomorphic Strings java (0) 2022.09.26