# 代码随想录Day4 链表基础2 LeetCodeT24 两两交换链表中的节点 LeetCode T19删除链表的倒数第N个节点 LeetCode面试题 链表相交 LeetCode 142 环形链表

### LeetCode T24: 两两交换链表中的节点

#### 1.思路1:

ListNode temp = current.next;
ListNode temp1 = current.next.next;
ListNode temp2 = current.next.next.next;
##### 交换:
current.next = temp1;
temp1.next = temp;
temp.next = temp2;
##### current向前移动
current = temp

#### 完整代码

/**
* 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 {
ListNode dummy = new ListNode(-1);
ListNode current = dummy;
while(current.next !=null && current.next.next != null)
{
ListNode temp = current.next;
ListNode temp1 = current.next.next;
ListNode temp2 = current.next.next.next;
current.next = temp1;
temp1.next = temp;
temp.next = temp2;
current  = temp;
}
return dummy.next;
}
}

### LeetCode T19 删除链表的倒数第N个节点

#### 解析代码:

/**
* 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 dummy = new ListNode(0);
ListNode fast = dummy;
ListNode slow = dummy;
while(n>0)
{
fast = fast.next;
n--;
}
while(fast.next != null)
{
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}

### LeetCode 面试题 链表相交

#### 代码

public class Solution {
int lenA = 0, lenB = 0;
while (curA != null) { // 求链表A的长度
lenA++;
curA = curA.next;
}
while (curB != null) { // 求链表B的长度
lenB++;
curB = curB.next;
}
// 让curA为最长链表的头，lenA为其长度
if (lenB > lenA) {
//1. swap (lenA, lenB);
int tmpLen = lenA;
lenA = lenB;
lenB = tmpLen;
//2. swap (curA, curB);
ListNode tmpNode = curA;
curA = curB;
curB = tmpNode;
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上（末尾位置对齐）
while (gap-- > 0) {
curA = curA.next;
}
// 遍历curA 和 curB，遇到相同则直接返回
while (curA != null) {
if (curA == curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
}

### LeetCode T142 环形链表

#### 代码:

/**
* class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) {
*         val = x;
*         next = null;
*     }
* }
*/
public class Solution {
while(fast != null && fast.next != null )
{
fast = fast.next.next;
slow = slow.next;
if(slow == fast)
{
ListNode index = fast;
while(index1 != index)
{
index = index.next;
index1 = index1.next;
}
return index;
}
}
return null;
}
}

|
4天前
|

[Java·算法·中等] LeetCode21. 合并两个有序链表
[Java·算法·中等] LeetCode21. 合并两个有序链表
10 2
|
6天前
|
Java Python

10 1
|
11天前
|

10 1
|
8天前
|

【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
6 0
|
1月前
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
24 0
|
11天前
|

LeetCode力扣第114题：多种算法实现 将二叉树展开为链表
LeetCode力扣第114题：多种算法实现 将二叉树展开为链表
8 0
|
11天前
|

LeetCode 题目 86：分隔链表
LeetCode 题目 86：分隔链表
14 2
|
15天前
|

【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
9 2
|
25天前
＜数据结构＞五道LeetCode链表题分析.环形链表，反转链表，合并链表，找中间节点.
＜数据结构＞五道LeetCode链表题分析.环形链表，反转链表，合并链表，找中间节点
23 1
|
11天前
|

LeetCode 83题：删除排序链表中的重复元素【面试】
LeetCode 83题：删除排序链表中的重复元素【面试】
5 0