大厂笔试题
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
2.9 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Node
  4. {
  5. int data;
  6. struct Node* next;
  7. };
  8. struct Node* head;
  9. // 在链表头节点插入一个值为x的节点
  10. void Insert(int x){
  11. struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
  12. temp->data = x;
  13. temp->next = head;
  14. // if(head != NULL)temp->next = head;
  15. head = temp;
  16. }
  17. // 迭代法实现反转列表
  18. struct Node* Reverse1(struct Node* head){
  19. struct Node *Current,*Prev,*next;
  20. Current = head;
  21. Prev = NULL;
  22. while(Current != NULL){
  23. next = Current->next;
  24. Current->next = Prev;
  25. Prev = Current;
  26. Current = next;
  27. }
  28. head = Prev;
  29. return head;
  30. }
  31. // 递归法实现反转列表
  32. void Reverse2(struct Node* p){
  33. if(p->next ==NULL){
  34. head = p;
  35. return;
  36. }
  37. Reverse2(p->next);
  38. struct Node* q = p->next;
  39. q->next = p;
  40. p->next = NULL;
  41. }
  42. // 链表内指定区间反转 调试好了
  43. struct Node* reverseBetween(struct Node* head, int m, int n ) {
  44. // write code here
  45. if(head == NULL)return head;
  46. if(head->next == NULL)return head;
  47. if(m == n)return head;
  48. struct Node *Current,*Prev,*next,*start,*start_last;
  49. int i;
  50. Current = head;
  51. Prev = NULL;
  52. next = NULL;
  53. // 先找到开始位置
  54. for (i=1; i<m; i++) {
  55. next = Current->next;
  56. // Current->next = Prev;
  57. Prev = Current;
  58. Current = next;
  59. }
  60. // 标记
  61. start_last = Prev;
  62. start = Current;
  63. // 反转
  64. for (i=0; i<(n-m+1); i++) {
  65. next = Current->next;
  66. Current->next = Prev;
  67. Prev = Current;
  68. Current = next;
  69. }
  70. // 头尾节点重指向
  71. if(start != head){
  72. start->next = next;
  73. start_last->next = Prev;//start!=head的情况下,需要保留start上一个指针
  74. }
  75. else {
  76. start->next = next;
  77. head = Prev;//start==head的情况下,直接将head指向待反转的最后一个
  78. }
  79. return head;
  80. }
  81. //打印链表的所有值
  82. void Print(){
  83. struct Node* temp = head;
  84. printf("List is:");
  85. while(temp){
  86. printf("%d",temp->data);
  87. temp = temp->next;
  88. }
  89. printf("\n");
  90. }
  91. void Print2(struct Node*p){
  92. if(p ==NULL){
  93. printf("\n");
  94. return;
  95. }
  96. // 正序打印
  97. // printf("%d",p->data);
  98. // Print2(p->next);
  99. // 反转打印
  100. printf("%d",p->data);
  101. Print2(p->next);
  102. }
  103. int main(){
  104. head = NULL;
  105. int n,x,i;
  106. printf("Please input the number of node:\n");
  107. scanf("%d",&n);
  108. for(i = 0;i<n;i++){
  109. printf("Please input the value of Node:\n");
  110. scanf("%d",&x);
  111. Insert(x);
  112. Print();
  113. }
  114. head = Reverse1(head);
  115. printf("Reverse linked list is:\n");
  116. Print();
  117. Reverse2(head);
  118. printf("Reverse linked list is:\n");
  119. Print();
  120. // head = reverseBetween(head,2,4);
  121. // Print();
  122. // printf("Print2 linked list is:\n");
  123. // Print2(head);
  124. // char name[100];
  125. // printf("What is your name?\n");
  126. // scanf("%s",name);
  127. // printf("Hello,%s,nice to meet you!\n",name);
  128. }