24. 两两交换链表中的节点
通过虚拟节点指定好头是什么
class Solution {
public ListNode swapPairs(ListNode head) {
if(head==null ||head.next == null){
return head;
}
ListNode dummy = new ListNode(-1,head);
ListNode temp1 = null;
ListNode temp2 = null;
ListNode curr = dummy;
while(curr.next!=null && curr.next.next!=null){
temp1 = curr.next;
temp2 = curr.next.next.next;
curr.next = curr.next.next;
temp1.next = temp2;
curr.next.next = temp1;
curr = curr.next.next;
}
return dummy.next;
}
}
19.删除链表的倒数第N个节点
创建虚拟头节点防止报空指针
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
//涉及链表一般使用快慢指针进行操作
ListNode dummy = new ListNode(-1,head);
ListNode fast = dummy;
ListNode slow = dummy;
for(int i = 0;i<=n;i++){
fast = fast.next;
}
while(fast!=null){
fast = fast.next;
slow = slow.next;
}
if(slow.next!=null){
slow.next =slow.next.next;
}
return dummy.next;
}
}
面试题 02.07. 链表相交
核心思想是比较两个头结点,同时要注意判断空值
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
//判断两个链表相交主要依靠指针进行遍历
int lenA = 0;
ListNode cur = headA;
if(headA==null ||headB==null){
return null;
}
//判断a链表的长度
while(cur.next!=null){
cur = cur.next;
lenA++;
}
int lenB = 0;
cur = headB;
//判断b链表的长度
while(cur.next!=null){
cur = cur.next;
lenB++;
}
ListNode curA = headA;
ListNode curB = headB;
//根据差值推进指针
for(int i=0;i<Math.abs(lenA-lenB);i++){
if(lenA>lenB){
curA=curA.next;
}else{
curB=curB.next;
}
}
//当两边指针推进到同一长度,开始计算是否相同
while(curA!=null){
if(curA==curB){
return curA;
}else{
curA= curA.next;
curB=curB.next;
}
}
return null;
}
}
142.环形链表II
使用快慢指针模拟前进时,慢指针在节点前前进了x,继续走了距离y相遇快指针,剩余距离z到节点,快指针前进了距离x+环长度a的倍数+y,快指针走了双倍慢指针的长度,所以可得:
2x+2y = x+na+y,即为x+y=n(y+z),x=(n-1)y+nz;这里n最大为2,(因为每次步进2,最坏的情况应该是刚好越过没相遇),所以应分两种情况来看。n=1,x=z;n=2,x=y+2z。y+z为一整圈,所以从相遇的地方让一个指针往节点走,和从起点走的一个指针以相同的速度,一定会在节点相遇。
public class Solution {
public ListNode detectCycle(ListNode head) {
//如果一个链表有环,那么快指针推进时会与慢指针相遇
if(head==null||head.next==null){
return null;
}
ListNode fast = head;
ListNode slow = head;
//快指针后面如果为空即停止
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
//需要两个节点分别记录头结点和相遇节点
ListNode x = head;
ListNode y = fast;
while(x!=y){
x=x.next;
y=y.next;
}
return x;
}
}
return null;
}
}

被折叠的 条评论
为什么被折叠?



