Compare commits

...

2 Commits

Author SHA1 Message Date
wsy182 c72996f342 Update Solution.java 2024-07-08 20:21:49 +08:00
wsy182 01f220bacf Create ListNode.java 2024-07-08 20:21:46 +08:00
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package solution;
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;
}
}

View File

@ -204,4 +204,26 @@ public class Solution {
}
return true;
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode(0);
ListNode current = dummy;
while (list1 != null && list2 != null) {
if (list1.val < list2.val) {
current.next = list1;
list1 = list1.next;
} else {
current.next = list2;
list2 = list2.next;
}
current = current.next;
}
if (list1 != null){
current.next = list1;
} else if (list2 != null){
current.next = list2;
}
return dummy.next;
}
}