/**
* 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) {
if (head == null || head.next == null) {
return head;
}
ListNode rest = head.next.next;
ListNode newHead = head.next;
newHead.next = head;
head.next = swapPairs(rest);
return newHead;
}
}
/**
* 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 dummyNode = new ListNode(0);
dummyNode.next = head;
ListNode prev = dummyNode;
// 只有当prev指向的结点之后有两个结点时才会进行交换
while (prev.next != null && prev.next.next != null) {
ListNode temp = head.next.next // 把next存在临时节点中
prev.next = head.next;
head.next.next = head;
head.next = temp;
prev = head; // 进1位
head = head.next; // 进1位
}
return dummyNode.next;
}
}
/**
* 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 dummyNode = new ListNode(0, head);
ListNode fastPointer = dummyNode;
ListNode slowPointer = dummyNode;
// 让快指针运动n步
while(n > 0) {
fastPointer = fastPointer.next;
n--;
}
while (fastPointer.next != null) {
fastPointer = fastPointer.next;
slowPointer = slowPointer.next;
}
// 此时慢指针的位置是, 要删除的前一个节点, 所以要删除当前节点的下一个节点
slowPointer.next = slowPointer.next.next;
return dummyNode.next;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
//ref. leetcode
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode currentA = headA;
ListNode currentB = headB;
int lenA = 0;
int lenB = 0;
while (currentA != null) { // 求链表A的长度
currentA = currentA.next;
lenA++;
}
while (currentB != null) { // 求链表B的长度
currentB = currentB.next;
lenB++;
}
currentA = headA;
currentB = headB;
if (lenB > lenA) {
//1. swap (lenA, lenB);
int tmpLen = lenA;
lenA = lenB;
lenB = tmpLen;
//2. swap (currentA, currentB);
ListNode tmpNode = currentA;
currentA = currentB;
currentB = tmpNode;
}
// 求长度差值
int gap = lenA - lenB;
// 让currentA和currentB的末尾对齐
while (gap-- > 0) {
currentA = currentA.next;
}
// 遍历currentA 和 currentB,如果不相同, 同时向后移动currentA和currentB, 如果相同则直接返回
while (currentA != null) {
if (currentA == currentB) {
return currentA;
}
currentA = currentA.next;
currentB = currentB.next;
}
return null;
}
}
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
// 走a+nb时候一定是在环的入口
// 第一次相遇的时候慢指针走了nb步
public class Solution {
public ListNode detectCycle(ListNode head) {
// 双指针问题, 有环意味着快慢指针会相遇
// 快指针每次走两个结点, 慢指针每次走一个节点
ListNode fast = head;
ListNode slow = head;
while(true) {
if (fast == null || fast.next == null) {
return null;
} else {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
break;
}
}
}
fast = head;
while(slow != fast) {
slow = slow.next;
fast = fast.next;
}
return fast;
}
}