0


数据结构——经典链表OJ(二)


乐观学习,乐观生活,才能不断前进啊!!!

我的主页:optimistic_chen
我的专栏:c语言
点击主页:optimistic_chen和专栏:c语言,
创作不易,大佬们点赞鼓励下吧~

文章目录

合并两个有序链表

合并两个有序链表———力扣
在这里插入图片描述

第一种思路

直接创建一个空链表,分别判断两个原链表的元素大小,升序插入到新链表中,但是此方法可能会超出时间限制(每一次都需要判断新链表的头节点是否为空),代码重复执行太多。

  1. typedefstructListNode ListNode;structListNode*mergeTwoLists(structListNode* list1,structListNode* list2){//判断为空链表if(list1==NULL){return list2;}if(list2==NULL){return list1;}
  2. ListNode* l1=list1;
  3. ListNode*l2=list2;//创建的新链表
  4. ListNode*newHead,*newTail;
  5. newHead=newTail=NULL;while(l1&&l2){if(l1->val < l2->val){//l1拿下来尾插if(newHead==NULL){
  6. newHead=newTail=l2;}else{
  7. newTail->next=l1;
  8. newTail=newTail->next;}
  9. l1=l1->next;}else{//l2拿下来尾插if(newHead==NULL){
  10. newHead=newTail=l2;}else{
  11. newTail->next=l2;
  12. newTail=newTail->next;}
  13. l2=l2->next;}}//跳出循环,要么l1先为空,要么l2先为空if(l2){
  14. newTail->next=l2;}if(l1){
  15. newTail->next=l1;}return newHead;}

第二种思路

优化:让新链表不为空,判断两个原链表元素大小后,直接插入到新链表中

  1. typedefstructListNode ListNode;structListNode*mergeTwoLists(structListNode* list1,structListNode* list2){//判断为空链表if(list1==NULL){return list2;}if(list2==NULL){return list1;}
  2. ListNode* l1=list1;
  3. ListNode*l2=list2;//创建的新链表(链表不为空)
  4. ListNode*newHead,*newTail;//newHead=newTail=NULL;
  5. newHead=newTail=(ListNode*)malloc(sizeof(ListNode));while(l1&&l2){if(l1->val < l2->val){//l1拿下来尾插
  6. newTail->next=l1;
  7. newTail=newTail->next;
  8. l1=l1->next;}else{//l2拿下来尾插
  9. newTail->next=l2;
  10. newTail=newTail->next;
  11. l2=l2->next;}}//跳出循环,要么l1先为空,要么l2先为空if(l2){
  12. newTail->next=l2;}if(l1){
  13. newTail->next=l1;}//动态申请的空间要手动释放掉
  14. ListNode* ret=newHead->next;free(newHead);
  15. newHead=NULL;return ret;}

环形链表的约瑟夫问题

环形链表的约瑟夫问题——牛客网
在这里插入图片描述
环形链表与我们平时见到的链表不同的是:他的尾节点的next指针指向头节点,而不是NULL。

在这里插入图片描述

  1. typedefstructListNode ListNode;//创建节点
  2. ListNode*buyNode(int x){
  3. ListNode*node=(ListNode*)malloc(sizeof(ListNode));if(node==NULL){exit(1);}
  4. node->val=x;
  5. node->next=NULL;return node;}
  6. ListNode*createCircle(int n){//先创建第一个节点
  7. ListNode*phead=buyNode(1);
  8. ListNode*ptail=phead;for(int i=2;i<=n;i++){
  9. ptail->next=buyNode(i);
  10. ptail=ptail->next;}//首位相连
  11. ptail->next=phead;return ptail;}intysf(int n,int m ){//第一步,根据n创建带环链表
  12. ListNode*prev=createCircle(n);
  13. ListNode*pcur=prev->next;//第二步记数int count=1;while(pcur->next!=pcur){if(count==m){//销毁pcur节点
  14. prev->next=pcur->next;free(pcur);
  15. pcur=prev->next;
  16. count=1;}else{
  17. prev=pcur;
  18. pcur=pcur->next;
  19. count++;}}//此时剩下的一个节点就是要返回的值return pcur->val;}

分割链表

分割链表——力扣
在这里插入图片描述

第一种思路

双指针法:
1.创建大,小两个新链表。
2.将小于特定值的节点放到小链表中,将大于等于特定值的节点放到大链表中
3.小链表的尾节点和大链表的第一个有效节点首尾相连

在这里插入图片描述
在这里插入图片描述

  1. typedefstructListNode ListNode;structListNode*partition(structListNode* head,int x){if(head==NULL){return head;}//创建两个带头链表
  2. ListNode*lessHead,*lessTail;
  3. ListNode*greaterHead,*greaterTail;
  4. lessHead=lessTail=(ListNode*)malloc(sizeof(ListNode));
  5. greaterHead=greaterTail=(ListNode*)malloc(sizeof(ListNode));//遍历原链表,将原链表中的节点尾插到大小链表中
  6. ListNode*pcur=head;while(pcur){if(pcur->val<x){//尾插到小链表中
  7. lessTail->next=pcur;
  8. lessTail=lessTail->next;}else{//尾插到大链表中
  9. greaterTail->next=pcur;
  10. greaterTail=greaterTail->next;}
  11. pcur=pcur->next;}//修改大链表的尾节点的next指针指向
  12. greaterTail->next=NULL;//小链表的尾节点和大链表的第一个有效节点首尾相连
  13. lessTail->next=greaterHead->next;
  14. ListNode*ret=lessHead->next;free(lessHead);free(greaterHead);
  15. lessHead=greaterHead=NULL;return ret;}

第二种思路

HeadNode哨兵结点:用于头插;Tail尾指针用于尾插;cur表示当前链表结点;
遍历链表依次用链表结点元素值与X比较;小于则头插;大于则尾插;
这里有一个小细节就是头插时,如果尾指针等于哨兵HeadNode则需更新Tail指向尾结点

  1. structListNode*partition(structListNode* head,int x){structListNode* HeadNode=(structListNode*)malloc(sizeof(structListNode));structListNode* Tail=HeadNode,*cur=head;
  2. Tail->next=NULL;while(cur){structListNode* next=cur->next;if(cur->val<x){
  3. cur->next=HeadNode->next;
  4. HeadNode->next=cur;if(Tail==HeadNode){
  5. Tail=cur;}}else{
  6. Tail->next=cur;
  7. Tail=cur;
  8. cur->next=NULL;}
  9. cur=next;}return HeadNode->next;}

完结

好了,这期的分享到这里就结束了~
如果这篇博客对你有帮助的话,可以点一个免费的赞并收藏起来哟~
可以点点关注,避免找不到我~
我们下期不见不散~~
这个链表题目还会继续,敬请期待~


本文转载自: https://blog.csdn.net/optimistic_chen/article/details/139216600
版权归原作者 optimistic_chen 所有, 如有侵权,请联系我们删除。

“数据结构——经典链表OJ(二)”的评论:

还没有评论