系列文章目录
24. 两两交换链表中的节点
①虚拟头节点法
/**
* 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(-1);// 设置一个虚拟头结点
dummyHead.next = head;// 将虚拟头结点指向head,这样方便后面做删除操作
ListNode pre = dummyHead;
ListNode temp;// 临时节点,保存两个节点后面的节点
ListNode firstnode; // 临时节点,保存两个节点之中的第一个节点
ListNode secondnode; // 临时节点,保存两个节点之中的第二个节点
while (pre.next!=null&&pre.next.next!=null){//先后顺序不能变
firstnode=pre.next;
secondnode=pre.next.next;
temp=pre.next.next.next;
pre.next=secondnode;
secondnode.next=firstnode;
firstnode.next=temp;
pre=firstnode;// cur移动,准备下一轮交换
}
return dummyHead.next;
}
}
②递归法以后有时间看
19.删除链表的倒数第N个节点
虚拟头节点+快慢指针
/**
* 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(-1);
dummyHead.next=head;
ListNode fast = dummyHead;
ListNode slow = dummyHead;
n++;
while (n-- > 0) {
fast = fast.next;
}
/* for (int i = 0; i < n; i++) {
fast = fast.next;
}*/
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
//此时 slow 的位置就是待删除元素的前一个位置。
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 lenA = 0, lenB = 0;
while (curA != null) {// 求链表A的长度
curA = curA.next;
lenA++;
}
while (curB != null) { // 求链表B的长度
lenB++;
curB = curB.next;
}
//注意:遍历完链表长度后curA和curB已指向链表末尾,需重置将两个指针指向链表的头节点
curA=headA;
curB=headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
ListNode tempNode = curA;
curA = curB;
curB = tempNode;
int tmpLen = lenA;
lenA = lenB;
lenB = tmpLen;
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
for (int i = 0; i < gap; i++) {
curA = curA.next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != 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) {//fast指针比slow指针快,故判断fast指针即可
fast = fast.next.next;
slow = slow.next;
// 快慢指针相遇,此时从head 和 相遇点,同时查找直至相遇
if (fast == slow) {// 有环
ListNode index1 = head;
ListNode index2 = fast;
// 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
while (index1 != index2) {
index1 = index1.next;
index2 = index2.next;
}
return index1;// 返回环的入口
}
}
return null;
}
}