24. 两两交换链表中的节点* 19.删除链表的倒数第N个节点*面试题 02.07. 链表相交142.环形链表II
24. 两两交换链表中的节点
题目地址 : 24. 两两交换链表中的节点 - 力扣(LeetCode)
Code :
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
//模块 化 处理
ListNode* pre = nullptr ;
ListNode* p1 = head ;
ListNode* p2 = nullptr ;
ListNode* Receive_NewLinkedListHead = nullptr ;
if(head == nullptr )
{
return head ;
}
if(head -> next == nullptr )
{
return head ;
}
p2 = p1 -> next ;
int FindAction = 0 ; // 观察哨
while(p1 != nullptr && p1->next != nullptr )
{
ListNode* next = p2->next ; // ( next , p->next ) 是否 为 空 指针 尚未 做 判断
if(FindAction == 0 ) // 首次 处理
{ // 第 一 组 结点 前 没有 结点 (无 头 链表 时 )
p1->next = next ;
p2->next = p1 ;
Receive_NewLinkedListHead = p2 ;
FindAction = 1 ;
}
else
{
pre->next = p2;
p1->next = next ;
p2->next = p1 ;
}
if(next == nullptr )
{
break ;
}
if(next -> next == nullptr )
{
break;
}
// 注意 更新 链表 情况
pre = p1 ;
p1 = next ;
p2 = next -> next ;
}
// 结点 交换 处理 后边 使用 的 是 相对 处理 / 支持 了 空指针 尾接 和 非空 结点 尾接 的 情况
return Receive_NewLinkedListHead ;
}
};
19.删除链表的倒数第N个节点
题目地址 : 19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
Code :
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* pre = nullptr ;
ListNode* p = head ;
ListNode* ptr_Detector = nullptr ;
int i;
int Find_Detector = 0;
if( head == nullptr )
{
return head ;
}
if( head -> next == nullptr && n == 1 )
{
return nullptr ;
}
ptr_Detector = head ;
for(i = 0 ; ptr_Detector != nullptr && i < n ; i++)
{
ptr_Detector = ptr_Detector -> next ;
}
while( ptr_Detector != nullptr )
{
ptr_Detector = ptr_Detector -> next ;
pre = p ;
p = p -> next ;
if(Find_Detector == 0)
{
Find_Detector = 1;
}
else
{
}
}
if(Find_Detector == 0)
{
head = head -> next ;
return head ;
}
pre -> next = p -> next ;
return head ;
}
};
面试题 02.07. 链表相交
题目地址 : 面试题 02.07. 链表相交 - 力扣(LeetCode)
Code :
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
// 长度 的 确定 , 长度 的 difference
// 结点 的 匹配
int length_LinkedList1 = 0;
int length_LinkedList2 = 0;
ListNode * p1 = headA;
ListNode * p2 = headB;
int diff_Length ;
while(p1 != nullptr )
{
length_LinkedList1 ++ ;
p1 = p1 -> next ;
}
while(p2 != nullptr )
{
length_LinkedList2 ++ ;
p2 = p2 -> next ;
}
p1 = headA;
p2 = headB;
diff_Length = length_LinkedList1 - length_LinkedList2 ;
if(diff_Length >= 0 )
{
for(int i = 0 ; i < diff_Length ; i++ )
{
p1 = p1 -> next ;
}
}
else
{
int abs_Diff_Length = abs(diff_Length);
for(int i = 0 ; i < abs_Diff_Length ; i++ )
{
p2 = p2 -> next ;
}
}
do
{
if(p1 == p2 ) // Check 地址 是否 一样
{
return p1 ;
}
// 不要 忘了 指针 的 移动
p1 = p1 -> next ;
p2 = p2 -> next ;
// "这是 一道 侧重 “ 积累 ” 的 题目 "
}while(p1 != nullptr );
return nullptr ;
}
};
142.环形链表II
题目地址 : 142. 环形链表 II - 力扣(LeetCode)
Code :
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
// 不同 的 移动 速度 ?
ListNode * p1 = head ;
ListNode * p2 = head ;
int diff_Pace = 0 ;
do
{
if(p1 == nullptr )
{
return nullptr ;
}
p1 = p1 -> next ;
if(p1 == nullptr )
{
return nullptr ;
}
p1 = p1 -> next ;
p2 = p2 -> next ;
diff_Pace ++ ;
if(p1 == p2 )
{
break;
}
}while(1) ;
// Check 环 外 的 PaceDiff 和 环 内 的 PaceDiff
// 位置 的 使用
// -> 环 的 长度
int length_Cycle = 0 ;
do
{
p2 = p2 -> next ;
length_Cycle ++ ;
}while(p2 != p1 );
ListNode * p3 = head ;
ListNode * p4 = head ;
for(int i = 0 ; i < length_Cycle ; i++ )
{
p3 = p3 -> next ;
}
while(p3 != p1)
{
p3 = p3 -> next ;
p4 = p4 -> next ;
}
// 交点 判断 ?
do
{
if(p4 == p2 )
{
return p2 ;
}
p4 = p4 -> next ;
p2 = p2 -> next ;
}while(1);
}
};
注意 首次处理 的 不同 , 注意 对 链表结点状态 的 更新 , 注意 对 链表末尾 的 考虑 , 指针的地址 是可以 进行比较的 (是否 相等 方面) , 注意 不要忘了 指针的移动 , 二刷 , 对 环形链表 入口 的 寻找 , (自己 主动) 疏通 逻辑 后 , 在 没有遇到 其它较多 的 障碍 / 问题 的 情况下 , 就通过了