Thanks to visit codestin.com Credit goes to github.com
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 d86c98b commit 07f0ea9Copy full SHA for 07f0ea9
java/21-Merge-Two-Sorted-Lists.java
@@ -0,0 +1,30 @@
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
+ final ListNode root = new ListNode();
14
+ ListNode prev = root;
15
+ while (list1 != null && list2 != null) {
16
+ if(list1.val < list2.val) {
17
+
18
19
+ prev.next = list1;
20
+ list1 = list1.next;
21
+ } else {
22
+ prev.next = list2;
23
+ list2 = list2.next;
24
+ }
25
+ prev = prev.next;
26
27
+ prev.next = list1 != null ? list1 : list2;
28
+ return root.next;
29
30
+}
0 commit comments