24. 两两交换链表中的节点
保存指针的操作还不够熟练,进行交换的时候一定要画图。笔记4,5,步骤交换的过程还有点模糊
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummyhead=new ListNode(0);
ListNode cur= dummyhead;//虚拟头节点作为当前节点
dummyhead.next=head;//虚拟头节点的下一个是真正的头节点
while(cur.next!=null&&cur.next.next!=null){//两个顺序不能交换否则发生空指针异常
//前者是偶数情况,后者是奇数情况。遍历终止
ListNode temp=cur.next;//保存节点 由虚拟节点指向节点一
ListNode temp1=cur.next.next.next;//保存节点 由 虚拟节点指向节点三
cur.next=cur.next.next;//将虚拟节点指向节点一的指针 指向 节点二
cur.next.next=temp; //将虚拟节点指向节点一的指针 指向 节点二
temp.next=temp1;//使节点一接下来 (temp.next)指向temp节点三(temp1指向节点三)
cur=cur.next.next;//当前指针向后移
}
return dummyhead.next;//返回真实头节点
}
}
19.删除链表的倒数第N个节点
我倒是没想到这个也可以这样做。凑到可以找到slow的cur节点的前一个就可以了
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyhead=new ListNode(0);
dummyhead.next=head;
ListNode fast=dummyhead;//快指针
ListNode slow=dummyhead;//慢指针
n++;
while(n-->0&&fast!=null){
fast=fast.next;//快指针先走你到n+1结点
}
while(fast!=null){
fast=fast.next;
slow=slow.next;//快慢指针一起遍历到fast遍历完且此时slow在n-1节点
}
slow.next=slow.next.next;//删除操作
return dummyhead.next;
}
}
面试题 02.07. 链表相交
其实还是没懂为什么是指针相等而不是值相等
从笔记可以看出来我老是犯低级错误
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA=headA;
ListNode curB=headB;
int Alen=0;//AB的长度
int Blen=0;
while(curA!=null){//求a的长度
Alen++;
curA=curA.next;
}
while(curB!=null){//求b的长度
Blen++;
curB=curB.next;
}
curA=headA;//将指针重新指向头结点
curB=headB;
//计算ab两条链表的长度差subL并使长的链表指针指向与短链表右对齐
if(Alen>Blen){
int subL=Alen-Blen ;
while(subL-->0){
curA=curA.next;
}
}
else{
int subL=Blen-Alen;
while(subL-->0){
curB=curB.next;
}
}
//AB链表指向空,说明不相交返回null。不指向空则同时遍历,指针相等时说明二者相交。返回对应指针值
while(curA!=null||curB!=null){
if(curA==curB){
return curA;
}
curA=curA.next ;
curB=curB.next ;
}
return null;
}
}
142.环形链表II
唯一的疑惑是为什么快指针是一次走两个节点,慢指针一次走一个节点。
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast=head;
ListNode slow=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;//慢指针走一步
fast=fast.next.next;//快指针走两步
if(slow==fast){
//快慢指针相遇时
ListNode index1=fast;//指针一从相遇的开始走
ListNode index2=head;//指针二从头开始走
while(index1!=index2){//指针一二各自遍历。相遇的地方就是环的入口
index1=index1.next;
index2=index2.next;
}
return index1;
}
}
return null;
}
}
手写笔记