1、给定一个链表,和一个数字,将链表分成左边区域小于该数字,中间区域等于该数字,右边区域大于该数字
解法思路:
1、设置小于区域的头和尾指针、等于区域的头和尾指针、大于区域的头和尾指针
2、遍历原链表,拿到第一个节点,将它和后面节点断开,判端是小于还是等于还是大于目标数字pivot,小于就赋给小于区域的头和尾,等于就赋给等于区域的头和尾,大于就赋给大于区域的头和尾,再有下一个元素,就让尾指针等于这个元素,尾指针向后走
3、判断如果有小于区域,就让小于区域的尾连到等于区域的头,如果没有等于区域的尾,就让等于区域的尾等于小于区域的尾,最后小于区域的尾巴直接连到大于区域的头,再判断如果有等于区域,就让等于区域的尾巴连向大于区域的头
4、返回头 从左向右依次判断,第3步,可以看做是判断尾指针,连接成串,第4步,可以看做是连接好了,返回头
public static ListNode solve(ListNode head,int pivot) {
ListNode sH = null; //小于头
ListNode sT = null; //小于尾
ListNode eH = null; //等于头
ListNode eT = null; //等于尾
ListNode mH = null; //大于头
ListNode mT = null; //大于尾
ListNode next = null;
while(head != null){
//保存头结点下个节点
next = head.next;
head.next = null;
if(head.val < pivot){
if(sH == null){
sH = head;
sT = head;
}else{
sT.next = head;
sT = head;
}
} else if(head.val == pivot){
if(eH == null){
eH = head;
eT = head;
}else{
eT.next = head;
eT = head;
}
} else if(head.val > pivot){
if(mH == null){
mH = head;
mT = head;
}else{
mT.next = head;
mT = head;
}
}
//把下个节点赋给头结点,就是头结点下移
head = next;
}
//如果有小于区域的尾结点,就连上中间区域的头结点,
if(sT != null){
sT.next = eH;
//如果没有中间区域,就把中间区域的尾结点赋给小于区域的尾结点,然后让它连接大于区域的头结点
eT = eT == null ? sT : eT;
}
//如果有中间区域,就让尾结点连接大于区域的头结点
if(eT != null){
eT.next = mH;
}
//返回头结点,从左向右依次判断,上面两个if是来做连接,然后连接好后,这个就返回头
return sH != null ? sH : (eH != null ? eH : mH);
}
2、复制含有随机指针节点的链表
class Node {
int value;
Node next;
Node rand; //随机指针
Node(int val){
value = val
}
}
rand指针是单链表节点结构中新增的指针,rand可能指向链表中的任意一个节点,也可能指向Null。给定一个由Node节点类型组成的无环单链表的头节点head,请实现一个函数完成这个链表的复制,并返回复制的新链表的头节点
[要求]时间复杂度O(N),额外空间复杂度O(1)
剑指offer-35
解法1:这个解法不满足题意,但是好理解
解法思路:
1、定义一个map,key存储链表的每一个节点,value是copy当前节点作为值,但是只有值,没有next和random指针
2、循环原链表,设置map中拷贝节点的next和random指针,都是从map中拿到原节点对应的next的值
public static Node solve(Node head) {
Map<Node,Node> map = new HashMap();
Node curr = head;
while(curr != null){
map.put(curr,new Node(curr.value));
}
while(curr != null){
map.get(curr).next = map.get(curr.next);
map.get(curr).rand = map.get(curr.rand);
}
return map.get(head);
}
解法二:符合题目要求
解法思路:原链表1->2->3
1、改造成1-》1’ ->2->2’->3->3’ ,改造好后,next指针就copy完了
2、copy random指针
3、将1’ ->2->2’->3->3’ 拆分成1->2->3和1’->2’->3’
public Node copyRandomList(Node head) {
if(head == null){
return null;
}
Node curr = head;
//构造next指针
while(curr != null){
Node tmp = new Node(curr.val);
tmp.next = curr.next;
curr.next = tmp;
curr = tmp.next;
}
//构造random指针
curr = head;
while(curr != null){
if(curr.random != null){
curr.next.random = curr.random.next;
}
curr = curr.next.next;
}
//拆分单链表
curr = head.next;
Node pre = head,res = head.next;
while(curr.next != null){
pre.next = pre.next.next;
curr.next = curr.next.next;
pre = pre.next;
curr = curr.next;
}
//原链表最后一个节点next置空,不然出错
pre.next = null;
return res;
}
3、链表中环的入口节点
剑指offer-22
解题思路:
1、快慢指针,快指针一次走两步,慢指针一次走一步,如果链表有环,两个指针一定会相遇,并且在环上最多就两圈,快指针就能赶上慢指针
2、相遇后,一个指针从头开始走,一个指针从相遇位置走,每次都走一步,最后相遇位置就是环的第一个入口节点,证明是图论,我并不懂
public ListNode detectCycle(ListNode head) {
//这个条件最好都有
if(head == null || head.next == null || head.next.next == null){
return null;
}
//这里还必须这样写,写成pre = head,cur = head.next会内存溢出,原因不清楚
ListNode pre = head.next;
ListNode cur = head.next.next;
while(pre != cur){
if(cur.next == null || cur.next.next == null){
return null;
}
pre = pre.next;
cur = cur.next.next;
}
//其中一个指针重置位头,两个继续向下走,直到相遇
cur = head;
while(cur != pre){
cur = cur.next;
pre = pre.next;
}
return cur;
}
以上所有内容是皆来自于算法课程
B站-左程云左神的教学视频