• C语言描述数据结构 —— 单链表OJ题


    1.移除链表元素

    对于这个题,我们有两种解题方法:

    方法一:我们要删除指定数值的结点,我们可以定义一个指针 cur ,这个指针用来负责遍历指定数值的结点,也是遍历结束的条件。那么还需要一个指针 prev ,这个指针负责指向 cur ,负责串联链表。如果没有 prev 指针,当指定数值的结点释放后,链表就“断”了。

    我们的代码就可以这样写:

    1. struct ListNode* removeElements(struct ListNode* head, int val)
    2. {
    3. struct ListNode* cur=head;
    4. struct ListNode* prev=NULL;
    5. while(cur)
    6. {
    7. if(cur->val == val)
    8. {
    9. //头删
    10. if(cur == head)
    11. {
    12. head = head->next;
    13. free(cur);
    14. cur=head;
    15. }
    16. //非头删
    17. else
    18. {
    19. cur=cur->next;
    20. free(prev->next);
    21. prev->next=cur;
    22. }
    23. }
    24. else
    25. {
    26. prev=cur;
    27. cur=cur->next;
    28. }
    29. }
    30. return head;
    31. }

    方法二:我们像操作数组那样,将不是指定数值结点复制到新的链表当中。这种思路无疑是最容易想到的。那么我们就需要一个新链表 newnode ,并且满足复制的结点都是尾插,为了方便尾插我们还需要一个用来存储尾结点地址的指针 tail ,这个方法我们在介绍单链表时说明过。那么我们就直接上代码了:

    1. struct ListNode* removeElements(struct ListNode* head, int val)
    2. {
    3. struct ListNode* newhead = NULL;//新链表的头
    4. struct ListNode* tail=NULL;//尾
    5. struct ListNode* cur=head;//遍历指定数值结点、结束条件
    6. while(cur)
    7. {
    8. //不是指定数值结点就尾插到新链表
    9. if(cur->val != val)
    10. {
    11. if(newhead == NULL)
    12. {
    13. newhead = tail = cur;
    14. }
    15. else
    16. {
    17. tail->next=cur;
    18. tail=tail->next;
    19. }
    20. cur=cur->next;
    21. }
    22. //如果是指定链表的结点就释放结点
    23. else
    24. {
    25. struct ListNode* del = cur;
    26. cur=cur->next;
    27. free(del);
    28. }
    29. }
    30. //如果测试用例提供的链表是空链表,没有 if 的话,就会报对空指针解引用的错误
    31. if(tail)
    32. tail->next=NULL;
    33. return newhead;
    34. }

    不过我们一定要注意野指针的问题,因为如果指定数值的结点在链表的最后一个结点,那么新链表的最后一个结点的指针指向的并不是一个空指针。

     那么对于方法二,我们可以在上面代码的基础上再简化一下代码。只不过我们需要使用一个新的链表,带头单链表,这个头也叫哨兵卫,是一个结点,这个结点是不存储任何有效数据的。那我们简化的代码就可以这么写:

    1. struct ListNode* removeElements(struct ListNode* head, int val)
    2. {
    3. struct ListNode* guard = (struct ListNode*)calloc(1,sizeof(struct ListNode));
    4. struct ListNode* cur=head;
    5. struct ListNode* tail=guard;
    6. while(cur)
    7. {
    8. if(cur->val != val)
    9. {
    10. tail->next=cur;
    11. tail=tail->next;
    12. cur=cur->next;
    13. }
    14. else
    15. {
    16. struct ListNode* del = cur;
    17. cur=cur->next;
    18. free(del);
    19. }
    20. }
    21. if(tail)
    22. tail->next=NULL;
    23. head = guard->next;
    24. free(guard);
    25. return head;
    26. }

    2.合并两个有序链表

    在解这个题之前我们先来了解一个算法,这个算法无论是顺序表还是链表都可以用。假设我们有两组数字,一组是 1、2、4,另一组是 1、3、4,我们现在要将他们合并,并且合并之后的顺序是升序排列。我们便可以这样做:

    我们有了这个算法理论基础,就可以对链表进行操作了。我们需要注意,我们将使用带头单向链表来完成这个题目,因为带有哨兵卫的链表可以少一个判空的环节,一定程度上减少了代码量。 

    1. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
    2. {
    3. struct ListNode* guard = (struct ListNode*)calloc(1,sizeof(struct ListNode));
    4. guard->next=NULL;//针对两个链表都是空链表的情况,避免野指针
    5. struct ListNode* cur1=list1;
    6. struct ListNode* cur2=list2;
    7. struct ListNode* tail = guard;
    8. while(cur1 && cur2)
    9. {
    10. if(cur1->val < cur2->val)
    11. {
    12. tail->next=cur1;
    13. tail=tail->next;
    14. cur1=cur1->next;
    15. }
    16. else
    17. {
    18. tail->next=cur2;
    19. tail=tail->next;
    20. cur2=cur2->next;
    21. }
    22. }
    23. //不管是哪个链表的数据没有放完
    24. if(cur1)
    25. tail->next=cur1;
    26. if(cur2)
    27. tail->next=cur2;
    28. struct ListNode* head=guard->next;
    29. free(guard);
    30. return head;
    31. }

     3.反转链表

    这道题我们也可以有两个思路,那么对于我们来说,最容易想到的就是直接改变箭头的方向。那么实现这样的操作也非常简单,我们看图来分析这个算法的原理。

     

    1. struct ListNode* reverseList(struct ListNode* head)
    2. {
    3. struct ListNode* n1=NULL;
    4. struct ListNode* n2=head;
    5. struct ListNode* n3=NULL;
    6. while(n2)
    7. {
    8. n3=n2->next;
    9. n2->next=n1;
    10. //迭代
    11. n1=n2;
    12. n2=n3;
    13. }
    14. return n1;
    15. }

     那么另一种思路更加巧妙,在我们之前介绍单链表了解了头插,那么对于这个题我们也可以使用头插。

    1. struct ListNode* reverseList(struct ListNode* head)
    2. {
    3. struct ListNode* cur = head;
    4. struct ListNode* next=NULL;
    5. struct ListNode* newhead = NULL;
    6. while(cur)
    7. {
    8. next = cur->next;
    9. cur->next=newhead;
    10. newhead=cur;
    11. cur=next;
    12. }
    13. return newhead;
    14. }

     4.链表的中间结点

    对于这题,我们一般都会这么去想,先遍历一遍链表,计算有多少个结点,然后除以 2 ,即可得到中间结点距离头结点有多远,然后再回到头结点再次遍历一遍链表,只不过这次遍历的长度是计算的距离。这个算法是比较差的,虽然简单,但是毕竟它遍历了两遍链表。那么现在我们介绍一个算法,即快慢指针,只需要遍历一遍,就可以得到中间结点的位置。

    1. struct ListNode* middleNode(struct ListNode* head)
    2. {
    3. struct ListNode* slow=head;
    4. struct ListNode* fast=head;
    5. while(fast && fast->next)
    6. {
    7. slow=slow->next;
    8. fast=fast->next->next;
    9. }
    10. return slow;
    11. }

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

     这个题与上一道题类似,我们可以采用遍历两边的方法来解决这道题。但是我们说了,没有必要。我们同样可以使用快慢指针的方法来解决这道题。

    1. struct ListNode* FindKthToTail(struct ListNode* pListHead, int k )
    2. {
    3. // write code here
    4. struct ListNode* fast=pListHead;
    5. struct ListNode* slow=pListHead;
    6. while(k--)
    7. {
    8. if(fast == NULL)//如果是空链表就直接返回空
    9. return fast;
    10. fast=fast->next;
    11. }
    12. while(fast)
    13. {
    14. fast=fast->next;
    15. slow=slow->next;
    16. }
    17. return slow;
    18. }

    6.链表分割

    我们学习过单链表的增删查改,那么这道题的关键就在于不能改变原来的数据顺序。如果我们定义一条新的链表的话,尾插就不会改变原来的数据顺序。而且上面介绍过带有哨兵卫的链表,那么这道题也可以使用这种链表。我们通过画图的形式来表达我的想法:

     

    我们有了想法之后,就可以用代码表达出来

    1. ListNode* partition(ListNode* pHead, int x)
    2. {
    3. // write code here
    4. struct ListNode* guard1 = (ListNode*)calloc(1,sizeof(ListNode));
    5. struct ListNode* guard2 = (ListNode*)calloc(1,sizeof(ListNode));
    6. struct ListNode* tail1=guard1;
    7. struct ListNode* tail2=guard2;
    8. struct ListNode* cur = pHead;
    9. //初始化,参数如果是空链表,避免野指针的问题
    10. tail1->next = NULL;
    11. tail2->next = NULL;
    12. while(cur)
    13. {
    14. if(cur->val < x)
    15. {
    16. tail1->next=cur;
    17. tail1=tail1->next;
    18. }
    19. else
    20. {
    21. tail2->next=cur;
    22. tail2=tail2->next;
    23. }
    24. cur=cur->next;
    25. }
    26. tail1->next = guard2->next;//链接
    27. tail2->next=NULL;//手动置空
    28. pHead = guard1->next;
    29. free(guard1);
    30. free(guard2);
    31. return pHead;
    32. }

    7.链表的回文结构

     这道题就是典型的判断回文,回文就是从前读和从后读的数据是一样的,比如 1、2、3、2、1,1、6、6、1 等。那么对于链表来说也是可以这样去读的。我们需要一个原链表逆置后的新链表,这个新链表再与原链表对比即可。

    1. bool chkPalindrome(ListNode* A) {
    2. // write code here
    3. struct ListNode* B = NULL;
    4. struct ListNode* cur = A;
    5. //头插法逆置链表
    6. while(cur)
    7. {
    8. struct ListNode* next = cur->next;
    9. cur->next=B;
    10. B=cur;
    11. cur=next;
    12. }
    13. while(A && B)
    14. {
    15. if(A->val != B->val)
    16. return false;
    17. A=A->next;
    18. B=B->next;
    19. }
    20. return true;
    21. }

    8.相交链表

    这道题的整体思路还是快慢指针,我们可以这样看相交链表:

     

    1. struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
    2. {
    3. struct ListNode* curA = headA;
    4. struct ListNode* curB = headB;
    5. int lenA=0;
    6. int lenB=0;
    7. while(curA)
    8. {
    9. lenA++;
    10. curA=curA->next;
    11. }
    12. while(curB)
    13. {
    14. lenB++;
    15. curB=curB->next;
    16. }
    17. //计算链表长度后指针位置归位
    18. curA=headA;
    19. curB=headB;
    20. //curA始终是长链表
    21. if(lenA < lenB)
    22. {
    23. struct ListNode* tmp =curA;
    24. curA=curB;
    25. curB=tmp;
    26. }
    27. //计算长链表先走几步
    28. int gap=abs(lenA-lenB);
    29. while(gap--)
    30. {
    31. curA=curA->next;
    32. }
    33. //找相交点
    34. while(curA != curB)
    35. {
    36. curA=curA->next;
    37. curB=curB->next;
    38. }
    39. return curA;
    40. }

    9.环形链表

    这道题目的要求是判断是否为环,我们最容易想到的方法是找到入环点然后在环绕一圈看环内是否有入环点。但事实上这种方法效率非常低,而且代码也比较复杂。正确的思路应该是快慢指针,将它看成一个追赶问题。我们引出这样一个问题:
     

     我们就运用这个思维,建立两个快慢指针。这两个指针一直在链表上走,那么必定会形成上面的问题,只要两个指针相遇,就可以证明链表有环。

    1. bool hasCycle(struct ListNode *head)
    2. {
    3. struct ListNode* fast=head;
    4. struct ListNode* slow=head;
    5. //如果不是环,那么必定有空
    6. while(fast && fast->next)
    7. {
    8. fast=fast->next->next;
    9. slow=slow->next;
    10. if(fast == slow)
    11. return true;
    12. }
    13. return false;
    14. }

    那么我们衍生一个问题,如果快指针的速度为 3 会怎么样?那么此时的相对速度就是 2 ,也就是每次行动,快指针会每次走两步,但是如果环的长度是一个奇数,那么最近的一次必定会是 1 ,但是快指针每次只走两步,此时快指针是永远不可能与慢指针重叠的。但如果环的长度是偶数,那么就必定会重叠。这是一个数学问题,大家仔细分析就能得到其中的逻辑。

    10.环形链表的相交结点

     这道题是上一道题的衍生,上一道题让我们证明环形链表,这道题让我们求环形链表的环入口。我们可以通过数学公式来分析,最后用代码实现。

    1. struct ListNode *detectCycle(struct ListNode *head) {
    2. struct ListNode* fast = head;
    3. struct ListNode* slow = head;
    4. struct ListNode* ret=NULL;//定义相遇点的新指针
    5. while(fast && fast->next)
    6. {
    7. fast = fast->next->next;
    8. slow=slow->next;
    9. //找到相遇点
    10. if(fast == slow)
    11. {
    12. ret = fast;
    13. while(ret && ret->next)
    14. {
    15. //先判断,再同时走,因为相遇点也可能是环的入口点
    16. if(ret == head)
    17. return ret;
    18. ret = ret->next;
    19. head = head->next;
    20. }
    21. }
    22. }
    23. return NULL;
    24. }

    那如果我们脑子并没有这么灵活,公式法想不到怎么办?我们还有另外一种方法,把这个问题转换为相交链表问题。这个思路简单,但是代码比较复杂。

    1. struct ListNode *detectCycle(struct ListNode *head) {
    2. struct ListNode* fast = head;
    3. struct ListNode* slow = head;
    4. struct ListNode* Next=NULL;//新链表
    5. while(fast && fast->next)
    6. {
    7. fast = fast->next->next;
    8. slow=slow->next;
    9. //找到相遇点
    10. if(fast == slow)
    11. {
    12. Next=fast->next;//新链表的起始位置
    13. fast->next=NULL;//断开链表
    14. }
    15. }
    16. struct ListNode* curA=head;
    17. struct ListNode* curB=Next;
    18. int lenA=0;
    19. int lenB=0;
    20. while(curA)
    21. {
    22. lenA++;
    23. curA=curA->next;
    24. }
    25. while(curB)
    26. {
    27. lenB++;
    28. curB=curB->next;
    29. }
    30. if(lenA
    31. {
    32. struct ListNode* tmp=head;
    33. head=Next;
    34. Next=tmp;
    35. }
    36. int gap=abs(lenA-lenB);
    37. while(gap--)
    38. {
    39. head=head->next;
    40. }
    41. while(head != Next)
    42. {
    43. head=head->next;
    44. Next=Next->next;
    45. }
    46. return head;
    47. return NULL;
    48. }

    11.复制带随机指针的链表

    可以发现,这道题的难点不在于复制,而是确定随机指针的指向位置。我们有两个思路,我们先谈谈第一个思路,这个思路可以说是一种暴力的解题方法。

     那么用代码描述也不困难:

    1. struct Node* copyRandomList(struct Node* head) {
    2. struct Node* cur=head;
    3. struct Node* tail=NULL;
    4. struct Node* newhead=NULL;
    5. //复制一条一模一样的链表
    6. while(cur)
    7. {
    8. struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
    9. newnode->val=cur->val;
    10. newnode->next=NULL;
    11. //尾插法复制结点
    12. if(tail == NULL)
    13. {
    14. tail = newnode;
    15. newhead=newnode;
    16. }
    17. else
    18. {
    19. tail->next=newnode;
    20. tail=tail->next;
    21. }
    22. //迭代
    23. cur = cur->next;
    24. }
    25. cur=head;//归位
    26. struct Node* copy=newhead;//记录新链表的头结点
    27. while(cur)
    28. {
    29. int num=0;
    30. struct Node* newcur=head;
    31. struct Node* tmp=newhead;
    32. //找每个结点random指向的位置相较于头结点的距离
    33. while(cur->random != newcur)
    34. {
    35. num++;
    36. newcur=newcur->next;
    37. }
    38. //在新链表中找到这个距离的位置
    39. while(num--)
    40. {
    41. tmp=tmp->next;
    42. }
    43. copy->random=tmp;
    44. //下一个结点
    45. copy=copy->next;
    46. cur=cur->next;
    47. }
    48. return newhead;
    49. }

    但是大家要明白一个点,这个算法的效率是非常低的,时间复杂度是 O(N^2) 。所以我们还有另外一种算法。

    那么另一种算法呢,极为巧妙,也是对上一种算法的优化,时间复杂度只有 O(N) 。效率大大提升,并且代码可读性也相对较高。那么现在我们就来分析一下这个算法。

     

     

     

    1. struct Node* copyRandomList(struct Node* head) {
    2. struct Node* cur=head;
    3. struct Node* copy = NULL;
    4. struct Node* next=NULL;
    5. //在结点后插入结点
    6. while(cur)
    7. {
    8. copy = (struct Node*)malloc(sizeof(struct Node));
    9. next=cur->next;
    10. cur->next=copy;
    11. copy->next=next;
    12. copy->val=cur->val;
    13. //迭代
    14. cur=next;
    15. }
    16. //建立random链接
    17. cur=head;
    18. while(cur)
    19. {
    20. copy=cur->next;
    21. next=copy->next;
    22. if(cur->random == NULL)
    23. {
    24. copy->random = NULL;
    25. }
    26. else
    27. {
    28. copy->random = cur->random->next;
    29. }
    30. //迭代
    31. cur=next;
    32. }
    33. //分解链表
    34. struct Node* newhead=NULL;
    35. struct Node* newtail=NULL;
    36. cur=head;
    37. while(cur)
    38. {
    39. copy=cur->next;
    40. next=copy->next;
    41. if(newtail == NULL)
    42. {
    43. newhead = newtail = copy;
    44. }
    45. else
    46. {
    47. newtail->next=copy;
    48. newtail=newtail->next;
    49. }
    50. cur->next=next;
    51. cur=next;
    52. }
    53. return newhead;
    54. }

  • 相关阅读:
    阿里云k8s环境下,因slb限额导致的发布事故
    【RHCE】ansible的简单配置
    [Python] 捕获异常
    【LIN总线测试】——LIN从节点物理层测试
    用c语言将文件中十六进制数据与二进制数据相互转换
    Android调用浏览器打开指定页面
    配置 Druid 数据源及密码加密-SpringBoot 2.7 实战基础
    【Rust 易学教程】第 1 天:Rust 基础,基本语法
    网站内的采集的外链该怎么进行本地化处理
    Revit基础知识:42个知识你知道吗?
  • 原文地址:https://blog.csdn.net/weixin_59913110/article/details/126245342