Create A23.java

This commit is contained in:
Mrhongxing committed 2026-10-16 01:03:47 +08:00
1 parent 8a0a7ccabb
commit ca4db34c41
1 file changed
+31
+31
View File
@@ -0,0 +1,31 @@
package com.hong.www;
public class A23 {
public ListNode mergeKLists(ListNode[] lists) {
if(lists == null || lists.length == 0) {
return new ListNode();
}
for (int i = lists.length - 1; i >=1; i=i-2) {
ListNode a = new ListNode();
ListNode head = a;
while (lists[i] != null && lists[i - 1] != null) {
if(lists[i].val < lists[i - 1].val) {
a.next = lists[i];
a = a.next;
lists[i] = lists[i].next;
}else {
a.next = lists[i - 1];
a = a.next;
lists[i - 1] = lists[i - 1].next;
}
}
a.next = lists[i] == null ? lists[i - 1] : lists[i];
lists[i - 1] = head.next;
lists[i] = null;
}
return lists[0];
}
}