当前位置:   article > 正文

数据结构:链表和经典链表OJ题合集(纯享版)

数据结构:链表和经典链表OJ题合集(纯享版)

 一、单向链表

1、链表的概念及结构

概念:链表是一种物理结构上非连续、非顺序的存储结构,数据结构的逻辑顺序是通过链表中的指针链接次序实现的。

就像图中的小火车,每节车厢都是一个节点,每个节点都存储着一个数据。它们本身并不是顺序存储的,而是由詹式车钩相互连起来的。而在链表中这个詹式车钩就是指针链接

火车在淡季时会减去几个车厢,而在旺季时会加上几个车厢。火车是可以灵活改变的。链表也是同理。

每个节点是在不同的空间存放,可能乱七八糟,但是有了指针链接将他们连接起来,从而在逻辑结构上好像是连续的。

在链表里,每节 "车厢" 是什么样子的呢?

与顺序表不同的是,每节 "车厢" 都是独立申请下来的,我们称之为 "节点/结点"。

节点的组成主要有两部分:当前节点要保存的数据和保存的下一个节点的地址(指针变量)。

图中指针变量plist保存的是第一个节点的地址,我们称plist此时 "指向" 第一个节点。如果我们想让plist指向第二个节点就让plist保存第二个节点的地址:0x0012FFA0。

为什么还需要指针变量来保存下一个节点的地址?

链表的每一个节点都是独立申请的(即需要插入一个数据时才去申请一块节点的空间),我们需要通过指针变量来保存下一个节点的位置才能从当前节点找到下一个节点。

2、单向链表的实现

结合前面学到的结构体知识,我们可以通过结构体来创建每个节点对应的结构体代码:

  1. typedef int SLTypeData;
  2. //创建每个节点类型
  3. typedef struct SList
  4. {
  5. SLTypeData data; //存储数据
  6. struct SList* next; //下一个节点的地址
  7. }*pList;

这就是链表每个节点的类型创建,关于单向链表的接口都有以下这些。

  1. void SLPushBack(pList* phead, SLTypeData val);//链表节点尾部插入
  2. pList SLBuyNode(int val);//申请节点空间
  3. void SLPrint(pList* phead);//打印链表每个节点的数据
  4. void SLPushFront(pList* phead, SLTypeData val);//链表节点头部插入
  5. void SLPopBack(pList* phead);//链表节点尾部删除
  6. void SLPopFront(pList* phead);//链表节点头部删除
  7. void SLInsert(pList* phead, pList pos, SLTypeData val);//指定位置之前插入
  8. pList SLfind(pList* phead, SLTypeData val);//查找该存储数据的节点并返回
  9. void SLInsertAfter(pList pos, SLTypeData val);//指定位置之后插入
  10. void SLEarse(pList* phead, pList pos);//指定位置节点删除
  11. void SLDesTroy(pList* phead);//链表销毁

完整代码:分别在三个文件,test.c文件调用函数、pList.c实现函数、pList.h函数声明。

pList.h 声明:

  1. #pragma once
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <stdlib.h>
  5. typedef int SLTypeData;
  6. //创建每个节点类型
  7. typedef struct SList
  8. {
  9. SLTypeData data; //存储数据
  10. struct SList* next; //下一个节点的地址
  11. }*pList;
  12. void SLPushBack(pList* phead, SLTypeData val);//链表节点尾部插入
  13. pList SLBuyNode(int val);//申请节点空间
  14. void SLPrint(pList* phead);//打印链表每个节点的数据
  15. void SLPushFront(pList* phead, SLTypeData val);//链表节点头部插入
  16. void SLPopBack(pList* phead);//链表节点尾部删除
  17. void SLPopFront(pList* phead);//链表节点头部删除
  18. void SLInsert(pList* phead, pList pos, SLTypeData val);//指定位置之前插入
  19. pList SLfind(pList* phead, SLTypeData val);//查找该存储数据的节点并返回
  20. void SLInsertAfter(pList pos, SLTypeData val);//指定位置之后插入
  21. void SLEarse(pList* phead, pList pos);//指定位置节点删除
  22. void SLDesTroy(pList* phead);//链表销毁

pList.c 函数实现:

  1. #include "pList.h"
  2. pList SLBuyNode(SLTypeData val)
  3. {
  4. pList ret = (pList)malloc(sizeof(struct SList));
  5. if (ret == NULL)
  6. {
  7. perror("malloc");
  8. return NULL;
  9. }
  10. ret->data = val;
  11. ret->next = NULL;
  12. return ret;
  13. }
  14. void SLPushBack(pList* phead, SLTypeData val)
  15. {
  16. assert(phead != NULL);
  17. pList node = SLBuyNode(val);
  18. if (node == NULL)
  19. {
  20. perror("SLBuyNode");
  21. return;
  22. }
  23. if (*phead == NULL)
  24. {
  25. *phead = node;
  26. return;
  27. }
  28. pList pcur = *phead;
  29. //找到尾节点
  30. while (pcur->next)
  31. {
  32. pcur = pcur->next;
  33. }
  34. //给尾结点下一个节点的地址
  35. pcur->next = node;
  36. }
  37. void SLPrint(pList* phead)
  38. {
  39. assert(phead);
  40. assert(*phead);
  41. pList pcur = *phead;
  42. while (pcur)
  43. {
  44. printf("%d -> ", pcur->data);
  45. pcur = pcur->next;
  46. }
  47. printf("NULL\n");
  48. }
  49. void SLPushFront(pList* phead, SLTypeData val)
  50. {
  51. assert(phead != NULL);
  52. pList node = SLBuyNode(val);
  53. if (node == NULL)
  54. {
  55. perror("SLBuyNode");
  56. return;
  57. }
  58. //头部插入操作
  59. node->next = *phead;
  60. *phead = node;
  61. }
  62. void SLPopBack(pList* phead)
  63. {
  64. assert(*phead != NULL);
  65. //首先判断一下链表是否只有一个节点的情况,如果是则直接释放头结点
  66. if ((*phead)->next == NULL)
  67. {
  68. free(*phead);
  69. *phead = NULL;
  70. return;
  71. }
  72. pList pcur = *phead;
  73. while (pcur->next->next)
  74. {
  75. pcur = pcur->next;
  76. }
  77. //完成尾部删除
  78. free(pcur->next);
  79. pcur->next = NULL;
  80. }
  81. void SLPopFront(pList* phead)
  82. {
  83. assert(phead != NULL);
  84. pList pcur = *phead;
  85. *phead = (*phead)->next;
  86. free(pcur);
  87. pcur = NULL;
  88. }
  89. void SLInsert(pList* phead, pList pos, SLTypeData val)
  90. {
  91. assert(phead);
  92. assert(*phead);
  93. assert(pos);
  94. pList node = SLBuyNode(val);
  95. //判断指定位置是否等于头结点
  96. if (pos == *phead)
  97. {
  98. node->next = *phead;
  99. *phead = node;
  100. return;
  101. }
  102. pList prve = *phead;
  103. //找到链表中pos位置之前的节点
  104. while (prve->next != pos)
  105. {
  106. prve = prve->next;
  107. }
  108. //插入操作
  109. prve->next = node;
  110. node->next = pos;
  111. }
  112. pList SLfind(pList* phead, SLTypeData val)
  113. {
  114. assert(phead);
  115. pList pcur = *phead;
  116. while (pcur)
  117. {
  118. if (pcur->data == val)
  119. {
  120. return pcur;
  121. }
  122. pcur = pcur->next;
  123. }
  124. return NULL;//如果遍历了一遍链表未找到存储该数据的节点则返回NULL
  125. }
  126. void SLInsertAfter(pList pos, SLTypeData val)
  127. {
  128. assert(pos);
  129. pList node = SLBuyNode(val);
  130. node->next = pos->next;
  131. pos->next = node;
  132. }
  133. void SLEarse(pList* phead, pList pos)
  134. {
  135. assert(phead);
  136. assert(*phead);
  137. assert(pos);
  138. if (pos == *phead)
  139. {
  140. *phead = (*phead)->next;
  141. free(pos);
  142. return;
  143. }
  144. pList prev = *phead;
  145. while (prev->next != pos)
  146. {
  147. prev = prev->next;
  148. }
  149. prev->next = pos->next;
  150. free(pos);
  151. pos = NULL;
  152. }
  153. void SLDesTroy(pList* phead)
  154. {
  155. assert(phead);
  156. assert(*phead);
  157. pList pcur = *phead;
  158. while (pcur)
  159. {
  160. pList next = pcur->next;
  161. free(pcur);
  162. pcur = next;
  163. }
  164. *phead = NULL;
  165. printf("销毁成功\n");
  166. }

test.c 函数调用:

  1. #include "pList.h"
  2. void SLttext()
  3. {
  4. pList plist = NULL;
  5. SLPushBack(&plist, 1);
  6. SLPushBack(&plist, 2);
  7. SLPushBack(&plist, 3);
  8. SLPushBack(&plist, 4);
  9. SLPrint(&plist); //打印:1 -> 2 -> 3 -> 4 -> NULL
  10. SLPushFront(&plist, 5);
  11. SLPushFront(&plist, 6);
  12. SLPushFront(&plist, 7);
  13. SLPrint(&plist);//打印:7 -> 6 -> 5 -> 1 -> 2 -> 3 -> 4 -> NULL
  14. SLPopBack(&plist);
  15. SLPrint(&plist);//打印:7 -> 6 -> 5 -> 1 -> 2 -> 3 -> NULL
  16. SLPopFront(&plist);
  17. SLPrint(&plist);//打印:6 -> 5 -> 1 -> 2 -> 3 -> NULL
  18. pList find = SLfind(&plist, 3);
  19. SLInsert(&plist, find, 11);
  20. SLPrint(&plist);//打印:6 -> 5 -> 1 -> 2 -> 11 -> 3 -> NULL
  21. find = SLfind(&plist, 1);
  22. SLInsertAfter(find, 12);
  23. SLPrint(&plist);//打印:6 -> 5 -> 1 -> 12 -> 2 -> 11 -> 3 -> NULL
  24. find = SLfind(&plist, 5);
  25. SLEarse(&plist, find);
  26. SLPrint(&plist);//打印:6 -> 1 -> 12 -> 2 -> 11 -> 3 -> NULL
  27. SLDesTroy(&plist);
  28. }
  29. int main()
  30. {
  31. SLttext();
  32. return 0;
  33. }

3、链表的分类

链表的结构非常多样,以下结构组合起来就有8种(2 x 2 x 2)链表结构。

链表结构:

1. 单向或双向

2. 带头或不带头

3. 循环或不循环

虽然有这么多链表的结构,但是我们实际中最常用的还是两种结构:单链表和双向带头循环链表

1. 不带头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。

2. 带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用链表数据结构,都是带头双向循环链表。另外这个结构虽然复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了,后面我们代码实现就知道了。

二、双向链表

1、双向链表的结构

注意:这里的 "带头" 跟前面我们说的 "头结点" 是两个概念,实际前面的在单链表阶段称呼不严谨,但是为了同学们更好的理解就直接称为单链表的头结点。

带头链表里的头结点,实际为 "哨兵位" ,哨兵位节点不存储任何有效元素,只是站在这里 "放哨的"。

"哨兵位" 存在的意义:

遍历循环链表避免死循环。

注意:双向链表中,哨兵位的下一个节点是链表的第一个节点(头结点)。哨兵位的前一个节点是链表的最后一个节点(尾结点)。所以双向链表的头插是在哨兵位的后面插入数据。尾插则是在哨兵位之前插入数据。

哨兵位是作为头结点和尾结点的中点,是头结点的起点也是尾节点的终点。这样解释更容易理解。

2、 双向链表的实现

List.h 链表函数链表节点类型的声明:

  1. #pragma once
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. typedef int SLDataType;
  6. typedef struct ListNode
  7. {
  8. SLDataType data;//存储数据
  9. struct ListNode* prve;//存储前一个节点的地址
  10. struct ListNode* next;//存储下一个节点的地址
  11. }SList;
  12. SList* SLInit();
  13. void SLPushBack(SList* phead, SLDataType x);//尾插
  14. void SLPrint(SList* phead);//显示链表数据
  15. void SLPustFront(SList* phead, SLDataType x);//头插
  16. void SLPopBack(SList* phead);//尾删
  17. void SLPopFront(SList* phead);//头删
  18. void SLInsert(SList* pos, SLDataType x);//指定位置插入
  19. SList* SLfind(SList* phead, SLDataType x);//查找节点
  20. void SLEarse(SList* pos);//指定位置删除
  21. void SLDestory(SList** pphead);//链表销毁

List.c 链表函数的实现:

  1. #include "List.h"
  2. //链表初始化
  3. SList* SLInit()
  4. {
  5. SList* phead = (SList*)malloc(sizeof(SList));
  6. if (phead == NULL)
  7. {
  8. perror("malloc error");
  9. return NULL;
  10. }
  11. phead->data = -1;
  12. //因为是循环链表,所以初始化要遵循循环格式
  13. phead->next = phead;
  14. phead->prve = phead;
  15. return phead;
  16. }
  17. //创建链表节点
  18. SList* ListBuyNode(SLDataType x)
  19. {
  20. SList* retNode = (SList*)malloc(sizeof(SList));
  21. if (retNode == NULL)
  22. {
  23. perror("malloc error");
  24. return NULL;
  25. }
  26. retNode->data = x;
  27. retNode->prve = retNode;
  28. retNode->next = retNode;
  29. return retNode;
  30. }
  31. //链表尾插
  32. void SLPushBack(SList* phead, SLDataType x)
  33. {
  34. assert(phead);
  35. SList* Node = ListBuyNode(x);
  36. Node->prve = phead->prve;
  37. Node->next = phead;
  38. phead->prve->next = Node;
  39. phead->prve = Node;
  40. }
  41. //链表数据显示
  42. void SLPrint(SList* phead)
  43. {
  44. assert(phead);
  45. SList* pcur = phead->next;
  46. while (pcur != phead)//哨兵位作为结束标识
  47. {
  48. printf("%d", pcur->data);
  49. if (pcur->next != phead)
  50. printf("->");
  51. pcur = pcur->next;
  52. }
  53. printf("\n");
  54. }
  55. //链表头插
  56. void SLPustFront(SList* phead, SLDataType x)
  57. {
  58. assert(phead);
  59. SList* Node = ListBuyNode(x);
  60. Node->next = phead->next;
  61. Node->prve = phead;
  62. phead->next->prve = Node;
  63. phead->next = Node;
  64. }
  65. //链表尾删
  66. void SLPopBack(SList* phead)
  67. {
  68. assert(phead);
  69. assert(phead->next != phead);
  70. SList* del = phead->prve;
  71. del->prve->next = phead;
  72. phead->prve = del->prve;
  73. free(del);
  74. del = NULL;
  75. }
  76. //链表头删
  77. void SLPopFront(SList* phead)
  78. {
  79. assert(phead);
  80. assert(phead->next != phead);
  81. SList* del = phead->next;
  82. del->next->prve = phead;
  83. phead->next = del->next;
  84. free(del);
  85. del = NULL;
  86. }
  87. //指定位置插入
  88. void SLInsert(SList* pos, SLDataType x)
  89. {
  90. assert(pos);
  91. SList* Node = ListBuyNode(x);
  92. Node->next = pos->next;
  93. Node->prve = pos;
  94. pos->next = Node;
  95. Node->next->prve = Node;
  96. }
  97. //查找节点
  98. SList* SLfind(SList* phead, SLDataType x)
  99. {
  100. assert(phead);
  101. SList* pcur = phead->next;
  102. while (pcur != phead)
  103. {
  104. if (pcur->data == x)
  105. {
  106. return pcur;
  107. }
  108. pcur = pcur->next;
  109. }
  110. return NULL;
  111. }
  112. //指定位置删除
  113. void SLEarse(SList* pos)
  114. {
  115. assert(pos);
  116. pos->prve->next = pos->next;
  117. pos->next->prve = pos->prve;
  118. free(pos);
  119. pos = NULL;
  120. }
  121. //链表销毁
  122. void SLDestory(SList** pphead)
  123. {
  124. assert(pphead && *pphead);
  125. SList* pcur = (*pphead)->next;
  126. while (pcur != *pphead)
  127. {
  128. SList* next = pcur->next;
  129. free(pcur);
  130. pcur = next;
  131. }
  132. free(*pphead);
  133. *pphead = NULL;
  134. }

test.c 函数的调用:

  1. #include "List.h"
  2. void SLtest()
  3. {
  4. SList* plist = NULL;
  5. plist = SLInit();
  6. //尾插
  7. SLPushBack(plist, 1);
  8. SLPushBack(plist, 2);
  9. SLPushBack(plist, 3);
  10. SLPushBack(plist, 4);
  11. SLPrint(plist);//打印:1->2->3->4
  12. //头插
  13. SLPustFront(plist, 5);
  14. SLPustFront(plist, 6);
  15. SLPustFront(plist, 7);
  16. SLPrint(plist);//打印:7->6->5->1->2->3->4
  17. //尾删
  18. SLPopBack(plist);
  19. SLPrint(plist);//打印:7->6->5->1->2->3
  20. //头删
  21. SLPopFront(plist);
  22. SLPrint(plist);//打印:6->5->1->2->3
  23. //指定位置插入
  24. SList* find = SLfind(plist, 5);
  25. SLInsert(find, 11);
  26. SLPrint(plist);//打印:6->5->11->1->2->3
  27. //指定位置删除
  28. find = SLfind(plist, 1);
  29. SLEarse(find);
  30. SLPrint(plist);//打印:6->5->11->2->3
  31. //链表销毁
  32. SLDestory(&plist);
  33. }
  34. int main()
  35. {
  36. SLtest();
  37. return 0;
  38. }

3、顺序表和双向链表的优缺点分析

不同点顺序表链表
存储空间上物理上一定连续逻辑上连续,但物理上不一定连续
随机访问支持O(1)不支持O(N)
任意位置插入或者删除元素可能需要搬移元素,效率低O(N)只需要修改指针指向
插入动态顺序表,空间不够时需要扩容没有容量的概念
应用场景元素高效存储+频繁访问任意位置插入和删除频繁

三、链表经典OJ题

 1、 移除链表元素

题目要求:

给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。

示例 1:

输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]

示例 2:

输入:head = [], val = 1
输出:[]

示例 3:

输入:head = [7,7,7,7], val = 7
输出:[]

解题思路:

思路1:

定义两个指针,一个指向当前节点,一个指向当前节点的下一个节点,如果下一个节点是要删除的节点就将当前节点的next存储下一个节点的再下一个节点的地址,然后free那个节点。知道遍历完原链表,该思路是改变原链表。

思路2:

建立一个新的链表,遍历原链表,将原链表中的非删除节点给新的链表,遍历结束后新链表中没有要删除节点。

  1. struct ListNode{
  2. int val;
  3. struct ListNode *next;
  4. };
  5. struct ListNode* removeElements(struct ListNode* head, int val) {
  6. struct ListNode* Newhead, * NewTail;//一个头链表,一个链表尾部
  7. Newhead = NewTail = NULL;
  8. //遍历原链表
  9. struct ListNode* pcur = head;
  10. while (pcur){
  11. if (pcur->val != val){
  12. if (Newhead == NULL){
  13. Newhead = NewTail = pcur;
  14. }
  15. else{
  16. NewTail->next = pcur;
  17. NewTail = NewTail->next;
  18. }
  19. }
  20. pcur = pcur->next;
  21. }
  22. //如果要删除值在原链表为节点,新链表的尾节点就不会指向NULL,所以这里要判断
  23. if (NewTail){
  24. NewTail->next = NULL;
  25. }
  26. return Newhead;
  27. }

2、反转链表

题目要求:

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

示例 2:

输入:head = [1,2]
输出:[2,1]

示例 3:

输入:head = []
输出:[]

进阶:链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?

解题思路:

可以定义三个新的节点类型的指针变量n1、n2、n3。然后就可以使用这三个指针来反转链表。首先n1初始化为NULL,n2初始化为head链表头结点,n3则是head头结点的下一个节点。然后循环n2->next = n1, n1 = n2, n2 = n3,n3 = n3->next。循环往复就完成了反转链表的操作。

  1. typedef struct ListNode{
  2. int val;
  3. struct ListNode *next;
  4. }*pList;
  5. //反转链表函数实现
  6. pList reverselist(pList head){
  7. if (head == NULL){
  8. return NULL;
  9. }
  10. pList n1, n2, n3;
  11. n1 = NULL, n2 = head, n3 = head->next;
  12. while (n2){
  13. n2->next = n1;
  14. n1 = n2;
  15. n2 = n3;
  16. if(n3)//如果n3为NULL就不再向后访问了
  17. n3 = n3->next;
  18. }
  19. return n1;
  20. }
  21. //以下调用上面函数的main是我自己写的,可以供大家参考
  22. int main()
  23. {
  24. //创建链表
  25. struct ListNode* phead, *pTail, *p;
  26. phead = pTail = NULL;
  27. int n = 0;
  28. printf("请输入要创建链表节点个数:>");
  29. scanf("%d", &n);
  30. int i = 0;
  31. for (i = 0; i < n; i++)
  32. {
  33. p = (pList)malloc(sizeof(struct ListNode));
  34. printf("请输入第%d个节点的值:>", i + 1);
  35. scanf("%d", &(p->val));
  36. p->next = NULL;
  37. if (phead == NULL)
  38. {
  39. phead = p;
  40. pTail = phead;
  41. }
  42. else
  43. {
  44. pTail->next = p;
  45. pTail = pTail->next;
  46. }
  47. }
  48. //调用反转链表函数
  49. pList Newhead = reverselist(phead);
  50. if (Newhead == NULL)
  51. {
  52. perror("reverselist");
  53. return;
  54. }
  55. phead = Newhead;
  56. //打印链表节点数据
  57. while (Newhead)
  58. {
  59. printf("%d->", Newhead->val);
  60. Newhead = Newhead->next;
  61. }
  62. printf("NULL\n");
  63. i = 1;
  64. Newhead = phead;
  65. //销毁链表
  66. while (Newhead)
  67. {
  68. phead = phead->next;
  69. free(Newhead);
  70. Newhead = phead;
  71. printf("已释放第%d条节点\n", i);
  72. i++;
  73. }
  74. return 0;
  75. }

3、合并两个有序链表

题目要求:

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 

示例 1:

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

示例 2:

输入:l1 = [], l2 = []
输出:[]

示例 3:

输入:l1 = [], l2 = [0]
输出:[0]

解题思路:

定义两个新的节点指针,一个头节点,一个尾结点。比较两个原链表当前节点的数据大小,然后插入新的链表,知道插完为止

  1. typedef struct ListNode {
  2. int val;
  3. struct ListNode* next;
  4. }*pList;
  5. //合并两个有序链表函数实现
  6. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
  7. if (list1 == NULL){
  8. return list2;
  9. }
  10. if (list2 == NULL){
  11. return list1;
  12. }
  13. struct ListNode* cur1, *cur2;
  14. cur1 = list1, cur2 = list2;
  15. struct ListNode* phead, * pTail;
  16. phead = pTail = (struct ListNode*)malloc(sizeof(struct ListNode));
  17. while (cur1 && cur2) {
  18. if (cur1->val < cur2->val){
  19. pTail->next = cur1;
  20. pTail = pTail->next;
  21. cur1 = cur1->next;
  22. }
  23. else{
  24. pTail->next = cur2;
  25. pTail = pTail->next;
  26. cur2 = cur2->next;
  27. }
  28. }
  29. if (cur1) {
  30. pTail->next = cur1;
  31. }
  32. if (cur2) {
  33. pTail->next = cur2;
  34. }
  35. struct ListNode* retList = phead->next;
  36. free(phead);
  37. return retList;
  38. }
  39. //以下创建链表调用函数的代码是自己写的,只为调用上面函数,不是链表规范创建。仅供参考
  40. int main()
  41. {
  42. struct ListNode* phead1, pTail1, p1;
  43. struct ListNode* phead2, pTail2, p2;
  44. phead1 = pTail1 = NULL;
  45. phead2 = pTail2 = NULL;
  46. int n = 0;
  47. printf("请输入要创建p1和p2链表节点个数:>");
  48. scanf("%d", &n);
  49. int i = 0;
  50. for (i = 0; i < n; i++)
  51. {
  52. p1 = (pList)malloc(sizeof(struct ListNode));
  53. p2 = (pList)malloc(sizeof(struct ListNode));
  54. printf("请输入p1链表第%d个节点的值:>", i + 1);
  55. scanf("%d", &(p1->val));
  56. printf("请输入p2链表第%d个节点的值:>", i + 1);
  57. scanf("%d", &(p2->val));
  58. p1->next = NULL;
  59. p2->next = NULL;
  60. if (phead1 == NULL && phead2 == NULL)
  61. {
  62. phead1 = p1;
  63. pTail1 = phead1;
  64. phead2 = p2;
  65. pTail2 = phead2;
  66. }
  67. else
  68. {
  69. pTail1->next = p1;
  70. pTail1 = pTail1->next;
  71. pTail2->next = p2;
  72. pTail2 = pTail2->next;
  73. }
  74. }
  75. pList Newhead = mergeTwoLists(phead1,phead2);
  76. if (Newhead == NULL)
  77. {
  78. perror("reverselist");
  79. return;
  80. }
  81. pList phead = Newhead;
  82. while (Newhead)
  83. {
  84. printf("%d->", Newhead->val);
  85. Newhead = Newhead->next;
  86. }
  87. printf("NULL\n");
  88. i = 1;
  89. Newhead = phead;
  90. while (Newhead)
  91. {
  92. phead = phead->next;
  93. free(Newhead);
  94. Newhead = phead;
  95. printf("已释放第%d条节点\n", i);
  96. i++;
  97. }
  98. return 0;
  99. }

4、链表的中间节点

题目要求:

给你单链表的头结点 head ,请你找出并返回链表的中间结点。

如果有两个中间结点,则返回第二个中间结点。

示例 1:

输入:head = [1,2,3,4,5]
输出:[3,4,5]
解释:链表只有一个中间结点,值为 3 。

示例 2:

输入:head = [1,2,3,4,5,6]
输出:[4,5,6]
解释:该链表有两个中间结点,值分别为 3 和 4 ,返回第二个结点。

解题思路:

快慢指针:使用快慢指针来遍历链表,慢指针每次走一步,快指针每次走两步,当快指针走到最后慢指针刚好走到中间节点。因为快指针是慢指针的2倍。所以快指针从起点到终点时慢指针就是这个路程的一半,是中点。

  1. typedef struct ListNode{
  2. int val;
  3. struct ListNode* next;
  4. }*pList;
  5. //函数实现
  6. struct ListNode* middleNode(struct ListNode* head) {
  7. if (head == NULL){
  8. return NULL;
  9. }
  10. struct ListNode* slow, *fast;
  11. slow = fast = head;
  12. //节点数可能是奇数个也可能是偶数个,所以要两种判断
  13. while (fast && fast->next) {
  14. slow = slow->next;
  15. fast = fast->next->next;
  16. }
  17. return slow;
  18. }

5、环形链表的约瑟夫问题

著名的Josephus问题

据说著名的历史学家 Josephus有过以下的故事:故事据说发生在古罗马时期,在罗马人占领乔塔帕特后,39个犹太人与约瑟夫及他的朋友躲到一个洞中,他们宁愿死也不要被敌人抓到,于是约定了一个自杀方式,41个人排成一个圆圈,由第1个人开始报数,每报数到第3人该人就必须自杀,然后再由下1个人重新报数,直到所有人都自杀身亡为止。

然而Josephus 和他的朋友并不想遵从,Josephus要他的朋友先假装遵从,他将朋友与自己安排在第16个与第31个位置,于是逃过了这场死亡游戏。

题目要求:

编号为 1 到 n 的 n 个人围成一圈。从编号为 1 的人开始报数,报到 m 的人离开。

下一个人继续从 1 开始报数。

n-1 轮结束以后,只剩下一个人,问最后留下的这个人编号是多少?

数据范围: 1≤ 本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】

推荐阅读
相关标签