作者: 华丞臧.
专栏:【数据结构】
各位读者老爷如果觉得博主写的不错,请诸位多多支持(点赞+收藏+关注
)。如果有错误的地方,欢迎在评论区指出。
推荐一款刷题网站 👉 LeetCode刷题网站
LeetCode.876 链表的中间结点
给定一个头结点为 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。
如果暴力求解,直接求链表长度再找中间结点程序过于繁琐,那么如何用更简单的方法求解呢?
快慢指针法:
既然题目要求链表中间结点,也就是说链表的 1/2
;那么在一次遍历链表的过程中,给两个指针slow
和fast
,slow
一次走一步fast
一次走两步,当fast
遍历完链表slow
停下的位置就是链表的中间结点。
当链表元素为奇数个时:
当链表元素为偶数个时:
struct ListNode* middleNode(struct ListNode* head)
{struct ListNode* slow = head; //慢指针struct ListNode* fast = head; //快指针while(fast && fast->next ){slow = slow->next;fast = fast->next->next;}return slow;
}
牛客----链表中倒数第k个结点
输入一个链表,输出该链表中倒数第k个结点。
输出链表中倒数第k
个结点,经典快慢指针法的问题;定义一个slow
指针一个fast
指针,先让fast
指针走指定的 k
步,然后再一起遍历链表,当fast
走完时slow
的位置就是链表中倒数第k个结点。
k
小于链表的长度时:k = 1
:k
大于或等于链表的长度时:k
大于链表长度时,返回空指针;当 k
等于链表长度时,返回 slow
。struct ListNode* FindKthToTail(struct ListNode* plisthead, int k ) {// write code herestruct ListNode* fast = plisthead;struct ListNode* slow = plisthead;while(k-- ){if(fast == NULL) //当k大于链表长度时{return NULL;}fast = fast->next;}while(fast){fast = fast->next;slow = slow->next;}return slow;
}
LeetCode.21 合并两个有序链表
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
创建一个新的链表头结点,依次比较传入的两个链表的大小,取其中的较小值尾插到新链表头结点后,并且让取出结点的那个链表的指针向后走一步再继续比较;循环此过程,当一个链表走到尾结束。
注意:当一个链表走完,另一个链表不一定走完,所以需要尾插剩下的那个链表。
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){struct ListNode* cur1 = list1;struct ListNode* cur2 = list2;struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));struct ListNode* cur = head;head->next = NULL;while(cur1 && cur2){if(cur1->val < cur2->val){cur->next = cur1;cur1 = cur1->next;cur = cur->next;}else{cur->next = cur2;cur2 = cur2->next;cur = cur->next;} }if(cur1){cur->next = cur1;}if(cur2){cur->next = cur2;}cur = head->next;free(head);head = NULL;return cur;
}
牛客.CM11 链表分割
现有一链表的头指针 ListNode* pHead
,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。
创建两个链表带头结点的链表 less
和 greater
,遍历传入的链表,小于 x
的结点尾插到 less
链表 当中,大于 x
的结点尾插到 greater
链表当中;遍历完链表,把greater中的结点尾插到less链表即可。
假设 x = 5
:
class Partition {
public:ListNode* partition(ListNode* pHead, int x) {// write code herestruct ListNode *less,*greater,*cur,*lesshead,*greaterhead;less = (struct ListNode*)malloc(sizeof(struct ListNode));greater = (struct ListNode*)malloc(sizeof(struct ListNode));lesshead = less;greaterhead = greater;cur = pHead;while(cur){if(cur->val < x){less->next = cur;less = less->next;}else{greater->next = cur;greater = greater->next;}cur = cur->next;}greater->next = NULL;less->next = greaterhead->next;less = lesshead;greater = greaterhead;lesshead = lesshead->next;free(less);free(greater);less = greater = NULL;return lesshead;}
};
牛客.OR36 链表的回文结构
对于一个链表,请设计一个时间复杂度为 O(n)
,额外空间复杂度为 O(1)
的算法,判断其是否为回文结构。
给定一个链表的头指针A,请返回一个 bool
值,代表其是否为回文结构。保证链表长度小于等于900。
测试样例:
通过上图可以看出,回文链表的前半段和后面的恰好相反,因为单向链表是单向的不能从链表两边往中间走;根据回文的特点,我们把后半段链表反转,然后一个指针从头部开始,一个指针从链表尾部开始,依次比较其结点的值是否相等当两个指针中间没有元素时,则表示链表是回文结构。
注意:这种思路会破坏原先链表的结构。
偶数项:
奇数项:
struct ListNode* middleNode(struct ListNode* head){struct ListNode* fast = head;struct ListNode* slow = head;while(fast && fast->next){fast = fast->next->next;slow = slow->next;}return slow;}struct ListNode* reverseList(struct ListNode* head){struct ListNode* cur = head;if(head == NULL ){return head;}else if(head->next == NULL){return head;}struct ListNode* ptr = head->next;cur->next = NULL;while(ptr){struct ListNode* tmp = ptr->next;ptr->next = cur;cur = ptr;ptr = tmp;}return cur;}class PalindromeList {public:bool chkPalindrome(ListNode* phead) {// write code herestruct ListNode* cur = phead;struct ListNode* mid = middleNode(phead);struct ListNode* rmid = reverseList(mid);while(rmid){if(cur->val != rmid->val){return false;}cur = cur->next;rmid = rmid->next;}return true;}
};
通过这些题目,学习了链表相关试题一些比较常用的方法:
- 双指针
- 快慢指针
并且在一些题目中,创建一个头结点可以很好地帮我们解决一些麻烦。