Skip to content

Commit 5b56357

Browse files
committed
add solution for merge two sorted lists
1 parent 60f8363 commit 5b56357

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
3+
4+
5+
6+
class Solution {
7+
8+
9+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
10+
ListNode merged = new ListNode();//๋จธ์ง€ ๋œ ๋…ธ๋“œ ๋ชจ์Œ
11+
ListNode temp = merged;
12+
while(list1 != null && list2 != null){
13+
14+
ListNode newNode = new ListNode();//์ƒˆ๋กœ์šด ๋…ธ๋“œ
15+
if (list1.val > list2.val){
16+
//list2
17+
newNode.val = list2.val;
18+
list2 =list2.next;//๋‹ค์Œ ๊ฐ€๋ฆฌํ‚ค๋„๋ก\
19+
// merged.next
20+
}else{
21+
newNode.val =list1.val;
22+
list1=list1.next;//๋‹ค์Œ ๊ฐ€๋ฆฌํ‚ค๋„๋ก
23+
}
24+
25+
merged.next = newNode;
26+
merged =merged.next;
27+
28+
}
29+
30+
if (list1==null)
31+
merged.next= list2;
32+
else if (list2 == null)
33+
merged.next=list1;
34+
35+
return temp.next;
36+
}
37+
}

0 commit comments

Comments
ย (0)