We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2ac98ae commit 9d7c188Copy full SHA for 9d7c188
merge-two-sorted-lists/hjeomdev.java
@@ -0,0 +1,28 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode() {}
7
+ * ListNode(int val) { this.val = val; }
8
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9
+ * }
10
+ */
11
+class Solution {
12
+ public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
13
+ ListNode list = new ListNode(-1);
14
+ ListNode result = list;
15
+ while (list1 != null && list2 != null) {
16
+ if (list1.val < list2.val) {
17
+ result.next = list1;
18
+ list1 = list1.next;
19
+ } else {
20
+ result.next = list2;
21
+ list2 = list2.next;
22
+ }
23
+ result = result.next;
24
25
+ result.next = list1 != null ? list1 : list2;
26
+ return list.next;
27
28
+}
0 commit comments