Post

[LeetCode 21] Merge Two Sorted Lists

LeetCode 21 (Java)
[Merge Two Sorted Lists] 문제 풀이

[LeetCode 21] Merge Two Sorted Lists

문제 바로가기


Description


You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.


Example 1


  • Input: list1 = [1,2,4], list2 = [1,3,4]
  • Output: [1,1,2,3,4,4]


Example 2


  • Input: list1 = [], list2 = []
  • Output: []


Example 3


  • Input: list1 = [], list2 = [0]
  • Output: [0]


Constraints


  • The number of nodes in both lists is in the range [0, 50].
  • -100 <= Node.val <= 100
  • Both list1 and list2 are sorted in non-decreasing order.








Code


내 제출


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null) return list2;
        if(list2 == null) return list1;

        ListNode result = new ListNode();
        ListNode temp = result;

        while(list1 != null && list2 != null) {
            if(list1.val > list2.val) {
                temp.next = list2;
                list2 = list2.next;
            }
            else {
                temp.next = list1;
                list1 = list1.next;
            }
            temp = temp.next;
        }

        if(list1 == null) {temp.next = list2;}
        else {temp.next = list1;}

        return result.next;
    }
}


RuntimeMemory
0 ms42.7 MB


다른 풀이


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode t=list1;
		 ListNode t1=list2;
		 ListNode ans=null;
		 ListNode ans_t=null;
		 while (t!=null ) {
			 if(ans==null) {
				 ans=new ListNode(t.val);
				 t=t.next;
				 ans_t=ans;
			 }
			 else {
				 ans_t.next=new ListNode(t.val);
				 t=t.next;
				 ans_t=ans_t.next;
			 }
		 }
		 while (t1!=null ) {
			 if(ans==null) {
				 ans=new ListNode(t1.val);
				 t1=t1.next;
				 ans_t=ans;
			 }
			 else {
				 ans_t.next=new ListNode(t1.val);
				 t1=t1.next;
				 ans_t=ans_t.next;
			 }
		 }

		 ListNode ans1=null;
		 while(ans!=null) {
			 if(ans1==null) {
				 ans1=new ListNode(ans.val);
				 ans=ans.next;
			 }
			 else {
				 if(ans.val<ans1.val) {
					 ans1=new ListNode(ans.val,ans1);
					 ans=ans.next;
				 }
				 else {
					 ListNode prev=ans1;
					 ListNode curr=ans1.next;
					 while(curr!=null) {
						 if(ans.val<curr.val){
							 break;
						 }
						 prev=curr;
						 curr=curr.next;
					 }
					 prev.next=new ListNode(ans.val,curr);
					 ans=ans.next;
				 }
			 }
		 }
		 return ans1;
    }
}


Reference


This post is licensed under CC BY 4.0 by the author.