链表反转
快慢指针解决链表问题
1.找链表的中间位置
一个指针一次走一步,一个指针一次走两步,当快指针走到头的时候,慢指针在中间位置
用途 :可以用来判断回文结构,从中间下一位置开始反转一半链表,然后和原先的链表一一比较,这种方式额外空间复杂度O(1);相比队列判断回文结构的额外空间复杂度为O(N)。
while(quick.next!=null && quick.next.next != null){
slow = slow.next;
quick = quick.next.next;
}
2.找链表的倒数第K个位置
快指针先走k-1步,然后快慢指针一块走,快指针到头时,慢指针到达倒数第k位置。这种方式只用遍历链表一次。(要注意代码鲁棒性)
//k要大于0,小于链表长度
public static Node lastKnode(Node head,int k){
if(head == null || k==0){
return null;
}
Node quick = head;
Node slow = head;
//quick 先走k-1步,要判断过程中不能出链表
for(int i = 0;i < k-1;i++){
if(quick == null) return null;
quick = quick.next;
}
while(quick.next != null){
quick = quick.next;
slow = slow.next;
}
return slow;
}
随机链表复制
//这种方法有问题
public RandomListNode Clone(RandomListNode pHead) {
if(pHead == null){
return null;
}
RandomListNode node = pHead;
RandomListNode cur = new RandomListNode(-1);
RandomListNode head = cur; //记录头节点前一节点
HashMap<RandomListNode,RandomListNode> map = new HashMap<>();
while(node != null){
cur.next = new RandomListNode(node.label);
// 存储的是原始的结点,而不是新产生的结点,后面对新结点连接random
//连接到了原先的结点上,是错误的
map.put(node,node.random);
cur = cur.next;
node = node.next;
}
cur = head.next;
//
while(cur != null){
cur.random = map.get(node);
cur = cur.next;
node = node.next;
}
return head.next;
}
两个链表相交问题和第一个公共点问题
//有环单链表是否相交
//时间复杂度O(N+M),额外空间复杂度O(1)
public boolean chkInter(ListNode head1, ListNode head2) {
// write code here3
ListNode f1 = findfirstNode(head1);
ListNode f2 = findfirstNode(head2);
//同一个结点入环
if(f1 == f2){
return true;
}
//不同结点入环,遍历环看是否找到另一个链表的入环点
ListNode cur = f1.next;
while(cur != f1){
if(cur==f2){
return true;
}
cur = cur.next;
}
return false;
}
//找到入环的第一个结点
public ListNode findfirstNode(ListNode head){
ListNode quick = head;
ListNode slow = head;
while(quick != null){
quick = quick.next.next;
slow = slow.next;
if(quick == slow){
break;
}
}
quick = head;
while(quick != slow){
quick = quick.next;
slow = slow.next;
}
return quick;
}
//判断无环链表是否相交
public class CheckIntersect {
// 相交的话,最后一个结点一定重合
public boolean chkIntersect(ListNode headA, ListNode headB) {
if(headA == null || headB == null){
return false;
}
while(headA.next != null){
headA = headA.next;
}
while(headB.next != null){
headB = headB.next;
}
return headB == headA ? true :false;
}
}
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
/*
HashSet<ListNode> set = new HashSet<>();
ListNode cur = pHead1;
while(cur != null && !set.contains(cur)){
set.add(cur);
cur = cur.next;
}
cur = pHead2;
HashSet<ListNode> nodeSet = new HashSet<>();
while (cur != null && !nodeSet.contains(cur)){
if(set.contains(cur)){
return cur;
}
nodeSet.add(cur);
cur = cur.next;
}
return null;
*/
// 空间复杂度O(1),确定一定会相交时
if(pHead1 == null || pHead2 == null){
return null;
}
ListNode p1 = pHead1;
ListNode p2 = pHead2;
while(p1 != p2){
p1 = p1==null ?pHead2:p1.next;
p2 = p2==null ?pHead1?p2.next;
}
return p1;
}
第一个相交点的问题:
1.用两个栈,分别把链表存进去,然后同时弹出来比较,直到最后一个相同的点,为第一个交点
2.长的链表比短的链表先走二者长度之差(如下)
3.两个链表两个指针一起走,第一个链表走完后接着第二个链表,第二个链表走完接着第一个链表走,二者相同的时候即交点(如下第二个)
// 如果两个链表都无环,返回第一个相交节点,如果不相交,返回null
public static Node noLoop(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
Node cur1 = head1;
Node cur2 = head2;
int n = 0;
while (cur1.next != null) {
n++;
cur1 = cur1.next;
}
while (cur2.next != null) {
n--;
cur2 = cur2.next;
}
if (cur1 != cur2) {
return null;
}
// n : 链表1长度减去链表2长度的值
cur1 = n > 0 ? head1 : head2; // 谁长,谁的头变成cur1
cur2 = cur1 == head1 ? head2 : head1; // 谁短,谁的头变成cur2
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
// 空间复杂度O(1),确定一定会相交时
if(pHead1 == null || pHead2 == null){
return null;
}
ListNode p1 = pHead1;
ListNode p2 = pHead2;
while(p1 != p2){
p1 = p1==null ?pHead2:p1.next;
p2 = p2==null ?pHead1?p2.next;
}
return p1;
}