add
This commit is contained in:
parent
a3b00e66e3
commit
84f1b5fa83
18
5.leetcode/src/com/fanxb/common/ListNode.java
Normal file
18
5.leetcode/src/com/fanxb/common/ListNode.java
Normal file
@ -0,0 +1,18 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
public class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
|
||||
public ListNode() {
|
||||
}
|
||||
|
||||
public ListNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
public ListNode(int val, ListNode next) {
|
||||
this.val = val;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
118
5.leetcode/src/com/fanxb/common/Q23.java
Normal file
118
5.leetcode/src/com/fanxb/common/Q23.java
Normal file
@ -0,0 +1,118 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Q23 {
|
||||
public static class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
|
||||
ListNode() {
|
||||
}
|
||||
|
||||
ListNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
ListNode(int val, ListNode next) {
|
||||
this.val = val;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
public ListNode mergeKLists(ListNode[] lists) {
|
||||
PriorityQueue<ListNode> queue = new PriorityQueue<>(Comparator.comparingInt(a -> a.val));
|
||||
queue.addAll(Arrays.stream(lists).filter(Objects::nonNull).collect(Collectors.toList()));
|
||||
if (queue.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
ListNode res = queue.poll(), temp = res;
|
||||
while (!queue.isEmpty()) {
|
||||
if (temp.next != null) {
|
||||
queue.add(temp.next);
|
||||
}
|
||||
temp.next = queue.poll();
|
||||
temp = temp.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 依次归并
|
||||
*/
|
||||
public ListNode so1(ListNode[] lists) {
|
||||
List<ListNode> listNodeList = Arrays.stream(lists).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
if (listNodeList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
ListNode res = listNodeList.get(0);
|
||||
for (int i = 1; i < listNodeList.size(); i++) {
|
||||
//依次和第i合并
|
||||
ListNode tempRes = new ListNode(), temp = tempRes;
|
||||
ListNode cur = listNodeList.get(i);
|
||||
while (res != null && cur != null) {
|
||||
if (res.val < cur.val) {
|
||||
temp.next = res;
|
||||
res = res.next;
|
||||
} else {
|
||||
temp.next = cur;
|
||||
cur = cur.next;
|
||||
}
|
||||
temp = temp.next;
|
||||
}
|
||||
//接上剩下的部分
|
||||
temp.next = res == null ? cur : res;
|
||||
//临时结果
|
||||
res = tempRes.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public ListNode so2(ListNode[] lists) {
|
||||
List<ListNode> listNodeList = Arrays.stream(lists).filter(Objects::nonNull).collect(Collectors.toList());
|
||||
int n = listNodeList.size();
|
||||
if (n == 0) {
|
||||
return null;
|
||||
}
|
||||
while (n > 1) {
|
||||
int count = 0;
|
||||
for (int i = 0; i + 1 < n; i += 2, count++) {
|
||||
ListNode a = listNodeList.get(i), b = listNodeList.get(i + 1), head = new ListNode(), temp = head;
|
||||
while (a != null && b != null) {
|
||||
if (a.val < b.val) {
|
||||
temp.next = a;
|
||||
a = a.next;
|
||||
} else {
|
||||
temp.next = b;
|
||||
b = b.next;
|
||||
}
|
||||
temp = temp.next;
|
||||
}
|
||||
temp.next = a == null ? b : a;
|
||||
listNodeList.set(count, head.next);
|
||||
}
|
||||
//如果为奇数,要把最后一个落单的放到count上
|
||||
if (n % 2 != 0) {
|
||||
listNodeList.set(count++, listNodeList.get(n - 1));
|
||||
}
|
||||
//新的链表数量为count
|
||||
n = count;
|
||||
}
|
||||
return listNodeList.get(0);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ListNode a = new ListNode(1);
|
||||
a.next = new ListNode(4);
|
||||
a.next.next = new ListNode(5);
|
||||
ListNode b = new ListNode(1);
|
||||
b.next = new ListNode(3);
|
||||
b.next.next = new ListNode(4);
|
||||
ListNode c = new ListNode(2);
|
||||
c.next = new ListNode(6);
|
||||
// new Q23().mergeKLists(new ListNode[]{a, b, c});
|
||||
// new Q23().so1(new ListNode[]{a, b, c});
|
||||
new Q23().so2(new ListNode[]{a, b});
|
||||
}
|
||||
}
|
56
5.leetcode/src/com/fanxb/common/Q25.java
Normal file
56
5.leetcode/src/com/fanxb/common/Q25.java
Normal file
@ -0,0 +1,56 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
public class Q25 {
|
||||
public ListNode reverseKGroup(ListNode head, int k) {
|
||||
ListNode res = new ListNode(), index = head;
|
||||
res.next = head;
|
||||
int count = 0;
|
||||
//left:开始翻转节点的父节点
|
||||
//right:结束翻转的节点
|
||||
ListNode beforeL = res, l = beforeL.next, r, afterR;
|
||||
while (index != null) {
|
||||
if (++count == k) {
|
||||
//进行翻转
|
||||
r = index;
|
||||
afterR = index.next;
|
||||
reverse(l, count);
|
||||
index = l;
|
||||
//处理头尾节点关系
|
||||
l.next = afterR;
|
||||
beforeL.next = r;
|
||||
//进行下一轮循环
|
||||
beforeL = index;
|
||||
l = index.next;
|
||||
count = 0;
|
||||
}
|
||||
index = index.next;
|
||||
}
|
||||
return res.next;
|
||||
}
|
||||
|
||||
/**
|
||||
* 翻转start后的n个节点
|
||||
*
|
||||
* @param start
|
||||
* @param n
|
||||
*/
|
||||
private void reverse(ListNode start, int n) {
|
||||
//反转节点
|
||||
ListNode prev = null;
|
||||
for (int i = 0; i < n; i++) {
|
||||
ListNode next = start.next;
|
||||
start.next = prev;
|
||||
prev = start;
|
||||
start = next;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ListNode node = new ListNode(1);
|
||||
node.next = new ListNode(2);
|
||||
node.next.next = new ListNode(3);
|
||||
node.next.next.next = new ListNode(4);
|
||||
node.next.next.next.next = new ListNode(5);
|
||||
new Q25().reverseKGroup(node, 3);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user