1 链表分割
1.1 题目介绍
现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。
题目连接:链表分隔
1.2 题目分析
1.2.1 基本思路
步骤一:申请两个链表lessHead,greaterHead并指向链表表尾元素的指针为lessTail,greaterTail;
步骤二:依次遍历链表,若当前节点的值小于x的节点插入lessHead中,否则插入greaterHead中;
步骤三:将greaterHead链表链接在lessHead链表中。
1.2.2 关键变量
- lessHead表示值小于x的链表表头
- lessTail表示值小于x的链表表尾
- greaterHead表示值大于等于x的链表表头
- greaterTail表示值大于等于x的链表表尾
1.3 程序代码
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
//创建两个链表
ListNode* lessHead , *lessTail , *greaterHead , *greaterTail ;
ListNode* cur = pHead ;
lessHead = lessTail = greaterHead = greaterTail = NULL ;
//将链表的分隔成两个链表
while(cur){
if(cur->val < x){
//链表为空时
if(lessTail == NULL){
lessHead = lessTail = cur ;
}else{
lessTail->next = cur ;
lessTail = lessTail->next ;
}
}else{
//链表为空时
if(greaterTail == NULL){
greaterHead = greaterTail = cur ;
}else{
greaterTail->next = cur ;
greaterTail = greaterTail->next ;
}
}
cur = cur->next ;
}
//合并两个链表
if(lessTail){
lessTail->next = greaterHead ;
}
if(greaterTail){
greaterTail->next = NULL;
}
if(lessHead){
return lessHead;
}else
return greaterHead ;
}
};
1.4 代码运行
2 链表的回文结构
2.1 题目介绍
对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。
示例:
1->2->2->1
返回:true
2.2 题目分析
2.2.1 基本思路
步骤一:找到链表的中间节点;
步骤二:逆转后半部分链表;
步骤三:比较前半部分链表与逆转后的后半部分链表,若两个链表元素相同则原链表为回文结构,否则不为回文结构。
2.2.2 关键变量
- slow 慢指针
- fast 快指针
2.3 程序代码
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
public:
bool chkPalindrome(ListNode* A) {
// write code here
//找到中间节点
ListNode* slow , *fast ;
slow = fast = A ;
if(fast == NULL || fast->next == NULL)
return true ;
while(fast && fast->next){
slow = slow->next ;
fast = fast->next->next ;
}
//反转后半部分链表
ListNode* cur , *prev ;
cur = slow ;
prev = NULL;
while(cur){
ListNode* next = cur->next ;
cur->next = prev ;
prev = cur ;
cur = next ;
}
//prev为后半部分的头节点
while(A && prev){
if(A->val != prev->val)
return false ;
else{
A = A->next ;
prev = prev->next ;
}
}
return true ;
}
};
2.4 代码运行
3 相交链表
3.1 题目介绍
编写一个程序,找到两个单链表相交的起始节点。如下面的两个链表:
在节点 c1 开始相交。
示例 :
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Reference of the node with value = 8
输入解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists
3.2 题目分析
3.2.1 基本思路
步骤一:计算两个链表的长度size1,size2;
步骤二:cur1 = headA , cur2 = headB
;
步骤三:长链表先走|size1 - size2|步;
步骤三:cur1、cur2同时向后遍历,若cur1 == cur2
,则该cur1指向的节点为第一个公共节点。
3.3 程序代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
/统计两个链表的长度
int size1 , size2 ;
size1 = size2 = 0 ;
struct ListNode* cur1 , *cur2 ;
cur1 = headA ;
cur2 = headB ;
while(cur1){
size1++ ;
cur1 = cur1->next ;
}
while(cur2){
size2++ ;
cur2 = cur2->next;
}
//长链表先走|size1 - size2|步
cur1 = headA ;
cur2 = headB ;
if(size1 > size2){
int len = size1- size2 ;
while(len--){
cur1 = cur1->next ;
}
}else{
int len = size2- size1 ;
while(len--){
cur2 = cur2->next ;
}
}
//同时向后遍历,两指针值相同为第一个公共节点
while(cur1 && cur2){
if(cur1 == cur2){
return cur1 ;
}
cur1 = cur1->next ;
cur2 = cur2->next ;
}
return NULL;
}
对代码进行改进
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
//统计两个链表的长度
int size1 , size2 ;
size1 = size2 = 0 ;
struct ListNode* cur1 , *cur2 ;
cur1 = headA ;
cur2 = headB ;
while(cur1){
size1++ ;
cur1 = cur1->next ;
}
while(cur2){
size2++ ;
cur2 = cur2->next;
}
//长链表先走|size1 - size2|步
int gap = abs(size1 - size2);
struct ListNode* longList = headA ,*shortList = headB ;
if(size2 > size1){
longList = headB ;
shortList = headA;
}
while(gap--){
longList = longList->next ;
}
//同时向后遍历,两指针值相同为第一个公共节点
while(longList && shortList){
if(longList == shortList)
return longList ;
longList = longList->next;
shortList = shortList->next;
}
return NULL;
}
3.4 代码运行
4 判断链表是否有环
4.1 题目介绍
给定一个链表,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
如果链表中存在环,则返回 true 。 否则,返回 false 。
示例 :
输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/linked-list-cycle
4.2 题目分析
4.2.1 基本思路
步骤一:设置快慢指针fast = slow = head
;
步骤二:快指针向前走两步,慢指针向前走一步;
步骤三:不断重复步骤二,若 fast == NULL || fast->next == NULL
,则该链表中没有环,若fast == slow
,则该链表中存在环。
4.2.2 关键变量
- fast快指针,每次走两步
- slow慢指针,每次走一步
4.3 程序代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
//设置快慢指针
struct ListNode* fast , *slow ;
fast = slow = head ;
if(fast == NULL || fast->next == NULL)
return false ;
//使快慢指针不断向前移动
while(fast && fast->next){
fast = fast->next->next ;
slow = slow->next ;
if(fast == slow)
return true ;
}
return false ;
}
4.4 代码运行
5 移除链表结构
5.1 题目介绍
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。
说明:不允许修改给定的链表。
示例 :
输入:head = [3,2,0,-4], pos = 1
输出:返回索引为 1 的链表节点
解释:链表中有一个环,其尾部连接到第二个节点。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/linked-list-cycle-ii
5.2 题目分析
5.2.1 基本思路
步骤一:设置快慢指针fast,slow,找到相遇点;
步骤二:一个指针从起点开始向后移动,一个指针从快慢指针的相遇点开始向后移动;
步骤三:返回两个指针的相遇点
5.2.2 关键变量
- fast 快指针
- slow 慢指针
5.3 程序代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head) {
if(head == NULL || head->next == NULL)
return NULL;
//找到快慢指针的相遇点
struct ListNode* slow , *fast ;
slow = fast = head ;
while(fast && fast->next){
fast = fast->next->next ;
slow = slow->next ;
if(fast == slow)
break;
}
if(fast != slow){
return NULL ;
}
//一个指针从起点开始向后移动,一个指针从快慢指针的相遇点开始向后移动;
struct ListNode* cur = head;
while(cur){
if(cur == fast)
return cur ;
cur = cur->next ;
fast = fast->next ;
}
return NULL;
}