经典软件面试题(2)

本文介绍了一种单向链表逆序的方法,通过设置两个前后相邻的指针p和q,将p所指向的节点作为q指向节点的后继,直至q为空,最后调整链表头和尾。附带实现及测试代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设链表节点为

  1. typedefstructtagListNode{
  2. intdata;
  3. structtagListNode*next;
  4. }ListNode,*List;

要求将一带链表头List head的单向链表逆序。

分析:

1). 若链表为空或只有一个元素,则直接返回;

2). 设置两个前后相邻的指针p,q. 将p所指向的节点作为q指向节点的后继;

3). 重复2),直到q为空

4). 调整链表头和链表尾

示例:以逆序A->B->C->D为例,图示如下

实现及测试代码如下:

[cpp:nogutter] view plain copy
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedefstructtagListNode{
  4. intdata;
  5. structtagListNode*next;
  6. }ListNode,*List;
  7. voidPrintList(Listhead);
  8. ListReverseList(Listhead);
  9. intmain()
  10. {
  11. //分配链表头结点
  12. ListNode*head;
  13. head=(ListNode*)malloc(sizeof(ListNode));
  14. head->next=NULL;
  15. head->data=-1;
  16. //将[1,10]加入链表

  17. ListNode*p,*q;
  18. p=head;
  19. for(inti=1;i<=10;i++)
  20. {
  21. q=(ListNode*)malloc(sizeof(ListNode));
  22. q->data=i;
  23. q->next=NULL;
  24. p->next=q;
  25. p=q;
  26. }
  27. PrintList(head);/*输出原始链表*/
  28. head=ReverseList(head);/*逆序链表*/
  29. PrintList(head);/*输出逆序后的链表*/
  30. return0;
  31. }
  32. ListReverseList(Listhead)
  33. {
  34. if(head->next==NULL||head->next->next==NULL)
  35. {
  36. returnhead;/*链表为空或只有一个元素则直接返回*/
  37. }
  38. ListNode*t=NULL,
  39. *p=head->next,
  40. *q=head->next->next;
  41. while(q!=NULL)
  42. {
  43. t=q->next;
  44. q->next=p;
  45. p=q;
  46. q=t;
  47. }
  48. /*此时q指向原始链表最后一个元素,也是逆转后的链表的表头元素*/
  49. head->next->next=NULL;/*设置链表尾*/
  50. head->next=p;/*调整链表头*/
  51. returnhead;
  52. }
  53. voidPrintList(Listhead)
  54. {
  55. ListNode*p=head->next;
  56. while(p!=NULL)
  57. {
  58. printf("%d",p->data);
  59. p=p->next;
  60. }
  61. printf("\n");
  62. }


设链表节点为

  1. typedefstructtagListNode{
  2. intdata;
  3. structtagListNode*next;
  4. }ListNode,*List;

要求将一带链表头List head的单向链表逆序。

分析:

1). 若链表为空或只有一个元素,则直接返回;

2). 设置两个前后相邻的指针p,q. 将p所指向的节点作为q指向节点的后继;

3). 重复2),直到q为空

4). 调整链表头和链表尾

示例:以逆序A->B->C->D为例,图示如下

实现及测试代码如下:

[cpp:nogutter] view plain copy
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedefstructtagListNode{
  4. intdata;
  5. structtagListNode*next;
  6. }ListNode,*List;
  7. voidPrintList(Listhead);
  8. ListReverseList(Listhead);
  9. intmain()
  10. {
  11. //分配链表头结点
  12. ListNode*head;
  13. head=(ListNode*)malloc(sizeof(ListNode));
  14. head->next=NULL;
  15. head->data=-1;
  16. //将[1,10]加入链表

  17. ListNode*p,*q;
  18. p=head;
  19. for(inti=1;i<=10;i++)
  20. {
  21. q=(ListNode*)malloc(sizeof(ListNode));
  22. q->data=i;
  23. q->next=NULL;
  24. p->next=q;
  25. p=q;
  26. }
  27. PrintList(head);/*输出原始链表*/
  28. head=ReverseList(head);/*逆序链表*/
  29. PrintList(head);/*输出逆序后的链表*/
  30. return0;
  31. }
  32. ListReverseList(Listhead)
  33. {
  34. if(head->next==NULL||head->next->next==NULL)
  35. {
  36. returnhead;/*链表为空或只有一个元素则直接返回*/
  37. }
  38. ListNode*t=NULL,
  39. *p=head->next,
  40. *q=head->next->next;
  41. while(q!=NULL)
  42. {
  43. t=q->next;
  44. q->next=p;
  45. p=q;
  46. q=t;
  47. }
  48. /*此时q指向原始链表最后一个元素,也是逆转后的链表的表头元素*/
  49. head->next->next=NULL;/*设置链表尾*/
  50. head->next=p;/*调整链表头*/
  51. returnhead;
  52. }
  53. voidPrintList(Listhead)
  54. {
  55. ListNode*p=head->next;
  56. while(p!=NULL)
  57. {
  58. printf("%d",p->data);
  59. p=p->next;
  60. }
  61. printf("\n");
  62. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值