1、反转链表
![在这里插入图片描述](https://img-blog.csdnimg.cn/6a29d4be65eb490db6fed610ddb0bf24.png)
public class Solution {
public ListNode ReverseList(ListNode head) {
if (head == null)
return head;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode p = head;
while (p.next != null) {
ListNode moved = p.next;
p.next = p.next.next;
moved.next = dummy.next;
dummy.next = moved;
}
return dummy.next;
}
}
public class Solution {
public ListNode ReverseList(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode node = ReverseList(head.next);
head.next.next = head;
head.next = null;
return node;
}
}
2、链表内指定区间反转
![在这里插入图片描述](https://img-blog.csdnimg.cn/35517b6dd3cc4319a730bcd3135ed759.png)
public class Solution {
public ListNode reverseBetween (ListNode head, int m, int n) {
if (head == null || head.next == null || m == n)
return head;
ListNode dummy = new ListNode(-1001);
dummy.next = head;
ListNode g = dummy;
int i = 1;
while (i < m) {
g = g.next;
i++;
}
ListNode p = g.next;
for (int step = 1; step <= n - m; step++) {
ListNode moved = p.next;
p.next = p.next.next;
moved.next = g.next;
g.next = moved;
}
return dummy.next;
}
}
public class Solution {
public ListNode reverseBetween (ListNode head, int m, int n) {
if (m == 1)
return reverseLeftN(head, n);
ListNode between = reverseBetween(head.next, m - 1, n - 1);
head.next = between;
return head;
}
ListNode successor = null;
public ListNode reverseLeftN(ListNode head, int n) {
if (n == 1) {
successor = head.next;
return head;
}
ListNode node = reverseLeftN(head.next, n - 1);
head.next.next = head;
head.next = successor;
return node;
}
}
3、链表中的节点每 k 个一组翻转
![在这里插入图片描述](https://img-blog.csdnimg.cn/30b49bc9caa04097ae5a5192079fa249.png)
public class Solution {
public ListNode reverseKGroup (ListNode head, int k) {
if (head == null || head.next == null || k == 1)
return head;
ListNode dummy = new ListNode(-1);
dummy.next = head;
int length = 0;
ListNode cur = head;
while (cur != null) {
length++;
cur = cur.next;
}
ListNode g = dummy, p = head;
for (int i = 0; i < length / k; i++) {
reverseBetween(g, p, k);
g = p;
p = p.next;
}
return dummy.next;
}
public void reverseBetween(ListNode g, ListNode p, int k) {
for (int step = 1; step < k; step++) {
ListNode moved = p.next;
p.next = p.next.next;
moved.next = g.next;
g.next = moved;
}
}
}
public class Solution {
public ListNode reverseKGroup (ListNode head, int k) {
if (head == null || head.next == null || k == 1)
return head;
ListNode tail = head;
for (int i = 0; i < k; i++) {
if (tail == null)
return head;
tail = tail.next;
}
ListNode node = reverse(head, tail);
head.next = reverseKGroup(tail, k);
return node;
}
public ListNode reverse(ListNode head, ListNode tail) {
ListNode prev = null, cur = null;
while (head != tail) {
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
}
4、合并两个排序的链表
![在这里插入图片描述](https://img-blog.csdnimg.cn/96fbdf58668e4efeb1d482935c2790a4.png)
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
cur.next = list1;
list1 = list1.next;
} else {
cur.next = list2;
list2 = list2.next;
}
cur = cur.next;
}
cur.next = list1 != null ? list1 : list2;
return dummy.next;
}
}
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
if (list1 == null)
return list2;
if (list2 == null)
return list1;
if (list1.val <= list2.val) {
list1.next = Merge(list1.next, list2);
return list1;
} else {
list2.next = Merge(list1, list2.next);
return list2;
}
}
}
5、合并 k 个已排序的链表
![在这里插入图片描述](https://img-blog.csdnimg.cn/539001412a85490dba4b5fee6106e40d.png)
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0)
return null;
int length = lists.length;
if (length == 1)
return lists[0];
if (length == 2)
return mergeTwoLists(lists[0], lists[1]);
ListNode[] left = new ListNode[(length + 1) / 2];
ListNode[] right = new ListNode[length / 2];
int i = 0;
for (; i < (length + 1) / 2; i++)
left[i] = lists[i];
int j = 0;
for (; i < length; i++)
right[j++] = lists[i];
return mergeTwoLists(mergeKLists(left), mergeKLists(right));
}
private ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null)
return list2;
if (list2 == null)
return list1;
ListNode dummy = new ListNode();
ListNode cur = dummy;
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
cur.next = list1;
list1 = list1.next;
} else {
cur.next = list2;
list2 = list2.next;
}
cur = cur.next;
}
cur.next = list1 != null ? list1 : list2;
return dummy.next;
}
}
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0)
return null;
if (lists.length == 1)
return lists[0];
PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b) -> Integer.compare(a.val, b.val));
for (ListNode list : lists) {
if (list != null)
pq.offer(list);
}
ListNode dummy = new ListNode();
ListNode cur = dummy;
while (!pq.isEmpty()) {
ListNode nextNode = pq.poll();
cur.next = nextNode;
cur = cur.next;
if (nextNode.next != null) {
pq.offer(nextNode.next);
}
}
return dummy.next;
}
}