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 86d5e72 commit d11e0caCopy full SHA for d11e0ca
1 file changed
merge-two-sorted-lists/Yiseull.java
@@ -0,0 +1,31 @@
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 head = new ListNode(-1);
14
+ ListNode current = head;
15
+
16
+ while (list1 != null && list2 != null) {
17
+ if (list1.val <= list2.val) {
18
+ current.next = list1;
19
+ list1 = list1.next;
20
+ } else {
21
+ current.next = list2;
22
+ list2 = list2.next;
23
+ }
24
+ current = current.next;
25
26
27
+ current.next = (list1 != null) ? list1 : list2;
28
29
+ return head.next;
30
31
+}
0 commit comments