题目链接:https://leetcode.cn/problems/swap-nodes-in-pairs/
/**
* 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 sentry = new ListNode(0, head);
ListNode pre = sentry;
ListNode cur = head;
ListNode last = head.next;
ListNode temp = new ListNode(0);
while (cur != null && last != null){
// 交换节点
cur.next = last.next;
last.next = cur;
pre.next = last;
// 指针移动
if (cur.next != null){
pre = cur;
cur = cur.next;
last = cur.next;
}else {
break;
}
}
return sentry.next;
}
}
时间复杂度:O(n)
空间复杂度:O(1)
题目链接:https://leetcode.cn/problems/remove-nth-node-from-end-of-list/
/**
* 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 pre = new ListNode(0, head);
ListNode cur = pre;
ListNode sign = head;
while (n > 1) {
sign = sign.next;
n--;
}
while (sign.next != null){
pre = pre.next;
sign = sign.next;
}
pre.next = pre.next.next;
return cur.next;
}
}
时间复杂度:O(n)
空间复杂度:O(1)
题目链接:https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/## 方法一
如果有节点交叉,从交叉节点往后,两个链表的长度肯定是相同的。
/**
* 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;
int lenB = 0;
while (curA != null) {
lenA++;
curA = curA.next;
}
while (curB != null) {
lenB++;
curB = curB.next;
}
while (lenA > lenB){
lenA --;
headA = headA.next;
}
while (lenB > lenA){
lenB --;
headB = headB.next;
}
while (headA != null){
if (headA == headB) {
return headA;
}else {
headA = headA.next;
headB = headB.next;
}
}
return null;
}
}
时间复杂度:O(m + n)
空间复杂度:O(1)
题目链接:https://leetcode.cn/problems/linked-list-cycle-ii/
使用快慢指针遍历链表,如果有相遇,则证明有环。2*(x + y) = x + y + n*(y + z) -> x = (n - 1)* (y + z) + z
如果有环,从相遇节点开始遍历,同时有另一指针从头节点遍历,相遇时的节点就是入口节点。
/**
* 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) {
//2 * (x + y) = x + y + N * (y + z);
//x + y = N * (y + z);
//x = (N - 1) * (y + z) + z;
//N = 1时; x = z;
//N > 1时; ((N - 1) * (y + z) + z) % (y + z) = z;
if (head == null || head.next == null) return null;
ListNode fast = head;
ListNode slow = fast;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if (fast == slow){ // 检测到有环
ListNode l1 = head;
ListNode l2 = fast;
while (l1 != l2){
l1 = l1.next;
l2 = l2.next;
}
return l1;
}
}
return null;
}
}
时间复杂度:O(N)
空间复杂度:O(1)
https://leetcode.cn/problems/linked-list-cycle-ii/