0


链表OJ题

文章目录

1. 链表的回文结构

链表的回文结构

因为单链表不能从后往前找节点,所以我们先找到中间节点,然后逆置,最后进行比较。

  1. #include<stdio.h>#include<stdbool.h>typedefstructListNode{int val;structListNode* next;}ListNode;structListNode*middleNode(structListNode* head){
  2. ListNode* slow,* fast;
  3. slow = fast = head;while(fast && fast->next){
  4. slow = slow->next;
  5. fast = fast->next->next;}return slow;}structListNode*reverseList(structListNode* head){if(NULL== head){return head;}
  6. ListNode* n1,* n2,* n3;
  7. n1 =NULL, n2 = head, n3 = head->next;while(n2){
  8. n2->next = n1;
  9. n1 = n2;
  10. n2 = n3;if(n3){
  11. n3 = n3->next;}}return n1;}
  12. bool chkPalindrome(ListNode* A){
  13. ListNode* mid =middleNode(A);
  14. ListNode* rmid =reverseList(mid);while(A && rmid){if(A->val != rmid->val){return false;}
  15. A = A->next;
  16. rmid = rmid->next;}return true;}

2. 相交链表

相交链表

  1. #include<stdio.h>#include<stdlib.h>structListNode{int val;structListNode* next;};structListNode*getIntersectionNode(structListNode* headA,structListNode* headB){structListNode* curA = headA;structListNode* curB = headB;int lenA =0;while(curA->next){++lenA;
  2. curA = curA->next;}int lenB =0;while(curB->next){++lenB;
  3. curB = curB->next;}//不相交if(curA != curB){returnNULL;}int gap =abs(lenA - lenB);//因为我们不知道A长还是B长,所以我们要用假设法,先假设A长,如果不对,再调换一下就行structListNode* longList = headA;structListNode* shortList = headB;if(lenB > lenA){
  4. longList = headB;
  5. shortList = headA;}//让长的先走gap步while(gap--){
  6. longList = longList->next;}//再同时走,找交点while(longList != shortList){
  7. longList = longList->next;
  8. shortList = shortList->next;}return longList;}

3. 链表中倒数第k个结点

链表中倒数第k个结点
思路2:

  1. #include<stdio.h>structListNode{int val;structListNoe* next;};structListNode*FindKthToTail(structListNode* pListHead,int k){structListNode* slow = pListHead,* fast = pListHead;//fast先走k步while(k--){//k还没有减到0,链表已经空了,说明k大于链表的长度if(NULL== fast){returnNULL;}
  2. fast = fast->next;}//slow和fast同时走,fast走到空,slow就是倒数第k个while(fast){
  3. slow = slow->next;
  4. fast = fast->next;}return slow;}

4. 环形链表

环形链表(1)
环形链表(2)

  1. #include<stdbool.h>structListNode{int val;structListNode* next;};
  2. bool hasCycle(structListNode* head){structListNode* slow = head,* fast = head;while(fast && fast->next){
  3. slow = slow->next;
  4. fast = fast->next->next;if(slow == fast){return true;}}return false;}

5. 环形链表 II

找入环点:
环形链表 II
法一:

  1. #include<stdio.h>structListNode{int val;structListNode* next;};structListNode*detectCycle(structListNode* head){structListNode* slow = head,* fast = head;while(fast && fast->next){
  2. slow = slow->next;
  3. fast = fast->next->next;if(slow == fast){structListNode* meet = slow;while(meet != head){
  4. meet = meet->next;
  5. head = head->next;}return meet;}}returnNULL;}

法二:

  1. #include<stdio.h>#include<stdlib.h>structListNode{int val;structListNode* next;};structListNode*getIntersectionNode(structListNode* headA,structListNode* headB){structListNode* curA = headA;structListNode* curB = headB;int lenA =0;while(curA->next){++lenA;
  2. curA = curA->next;}int lenB =0;while(curB->next){++lenB;
  3. curB = curB->next;}//不相交if(curA != curB){returnNULL;}int gap =abs(lenA - lenB);structListNode* longList = headA;structListNode* shortList = headB;if(lenB > lenA){
  4. longList = headB;
  5. shortList = headA;}//让长的先走gap步while(gap--){
  6. longList = longList->next;}//再同时走,找交点while(longList != shortList){
  7. longList = longList->next;
  8. shortList = shortList->next;}return longList;}structListNode*detectCycle(structListNode* head){structListNode* slow = head,* fast = head;while(fast && fast->next){
  9. slow = slow->next;
  10. fast = fast->next->next;if(slow == fast){structListNode* meet = slow;structListNode* headx = meet->next;
  11. meet->next =NULL;returngetIntersectionNode(head, headx);}}returnNULL;}

6. 随机链表的复制

随机链表的复制
写代码的时候建议一边画细图,一边写:
随机链表的复制细图

  1. #include<stdio.h>#include<stdlib.h>structNode{int val;structNode*next;structNode*random;};structNode*copyRandomList(structNode* head){structNode* cur = head;//拷贝节点插入原节点的后面while(cur){structNode* copy =(structNode*)malloc(sizeof(structNode));
  2. copy->val = cur->val;//插入
  3. copy->next = cur->next;
  4. cur->next = copy;//迭代
  5. cur = cur->next->next;}//控制拷贝节点的random
  6. cur = head;while(cur){structNode* copy = cur->next;if(NULL== cur->random){
  7. copy->random =NULL;}else{
  8. copy->random = cur->random->next;}//迭代
  9. cur = cur->next->next;}//把copy节点解下来,链接成新链表structNode* copyhead =NULL,* tail =NULL;
  10. cur = head;while(cur){structNode* copy = cur->next;structNode* next = copy->next;//尾插if(NULL== tail){
  11. copyhead = tail = copy;}else{
  12. tail->next = copy;
  13. tail = tail->next;}
  14. cur->next = next;
  15. cur = next;}return copyhead;}

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

“链表OJ题”的评论:

还没有评论