21.合并两个有序链表
题目里说拼接,那应该不能复制,地址不能变,这个题目不知道做过多少次了,看到这个总会想到归并排序。
迭代
自己写的和官方的迭代算是一样的
java对象赋值、深拷贝与浅拷贝_java 对象赋值是浅拷贝还是深拷贝-CSDN博客
返回值怎么改一下就不对了,按理说指向也没变啊,为什么第一个return的写法不对,因为p1和list都指向的一个地址,next时都next了,list1也丢失了。
递归
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
} else if (l2 == null) {
return l1;
} else if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}
23.合并K个升序链表
两两合并
做了上一题很容易可以想到这个方法
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
ListNode ans = null;
for (int i = 0; i < lists.length; ++i) {
ans = mergeTwoLists(ans, lists[i]);
}
return ans;
}
public ListNode mergeTwoLists(ListNode a, ListNode b) {
if (a == null || b == null) {
return a != null ? a : b;
}
ListNode head = new ListNode(0);
ListNode tail = head, aPtr = a, bPtr = b;
while (aPtr != null && bPtr != null) {
if (aPtr.val < bPtr.val) {
tail.next = aPtr;
aPtr = aPtr.next;
} else {
tail.next = bPtr;
bPtr = bPtr.next;
}
tail = tail.next;
}
tail.next = (aPtr != null ? aPtr : bPtr);
return head.next;
}
}
归并两两合并
这和第一个的时间是不一样的,举个大点的例子可以看出来。
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
return merge(lists, 0, lists.length - 1);
}
public ListNode merge(ListNode[] lists, int l, int r) {
if (l == r) {
return lists[l];
}
if (l > r) {
return null;
}
int mid = (l + r) >> 1;
return mergeTwoLists(merge(lists, l, mid), merge(lists, mid + 1, r));
}
public ListNode mergeTwoLists(ListNode a, ListNode b) {...}
}
优先队列
class Solution {
class Status implements Comparable<Status> {
int val;
ListNode ptr;
Status(int val, ListNode ptr) {
this.val = val;
this.ptr = ptr;
}
public int compareTo(Status status2) {
return this.val - status2.val;
}
}
PriorityQueue<Status> queue = new PriorityQueue<Status>();
public ListNode mergeKLists(ListNode[] lists) {
for (ListNode node: lists) {
if (node != null) {
queue.offer(new Status(node.val, node));
}
}
ListNode head = new ListNode(0);
ListNode tail = head;
while (!queue.isEmpty()) {
Status f = queue.poll();
tail.next = f.ptr;
tail = tail.next;
if (f.ptr.next != null) {
queue.offer(new Status(f.ptr.next.val, f.ptr.next));
}
}
return head.next;
}
}
自己看看优先队列和重写compareTo方法吧。
148.排序链表
说一下自己的思路吧,把链表节点放到哈希表里,然后对链表的值放在数组里之后排序之后,通过val获取到哈希表里的节点然后串一起。遇到问题:
- 有环:要在节点放入集合的时候置空,不然它的next指针还是存在的,后面串一起就会有环
- 重复元素:hashmap对于重复元素的处理。反正是key肯定是唯一的啊,先判断集合里是否存在这个值,存在的话直接加到原来节点的后面(这是存在第二个,更多的话要加到结尾)
class Solution {
public ListNode sortList(ListNode head) {
if(head == null || head.next ==null)return head;
ListNode prehead = new ListNode();
ListNode res = prehead ;
List<Integer> ls = new ArrayList<>();
HashMap<Integer, ListNode> temp = new HashMap<>();
while(head != null) {
if(!temp.containsKey(head.val))ls.add(head.val);
//解决相同的key
if(temp.containsKey(head.val)){
//这里原来只考虑了一个相同节点,第三个的时候呢?
ListNode x = temp.get(head.val);
while(x.next != null)x = x.next;
x.next = head;
ListNode k = head.next;
head.next = null;
head = k;
continue;
}
temp.put(head.val, head);
//解决环
ListNode k = head.next;
head.next = null;
head = k;
}
Collections.sort(ls);
for(int i = 0; i < ls.size(); i++){
prehead.next = temp.get(ls.get(i));
prehead = prehead.next;
//多个重复
while(prehead.next != null)prehead = prehead.next;
}
return res.next;
}
}
现在我已经很懒了,主要自顶向下和自底向上的部分吧,公共部分还是合并两个。
关于自底向上还是想写一点自己的理解:
对于每一趟(每个固定的subLength),先选择出两个长度为subLength的链表后(每选出一个要记得结尾next置空),记录第二个sublength长度的最后一个节点的下一个next;合并选出的两个后继续。
本质上还是归并排序。花费太多时间了,今天先这样吧。