1
0

lists.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // Created by 李洋 on 2023/8/6.
  3. //
  4. #ifndef LEECODE_C_LISTS_H
  5. #define LEECODE_C_LISTS_H
  6. #pragma once
  7. #include <iostream>
  8. #include <random>
  9. struct ListNode {
  10. int val;
  11. ListNode *next;
  12. ListNode() : val(0), next(nullptr) {}
  13. ListNode(int x) : val(x), next(nullptr) {}
  14. ListNode(int x, ListNode *next) : val(x), next(next) {}
  15. void list() {
  16. ListNode *temp = this;
  17. while (temp) {
  18. std::cout << temp->val << " ";
  19. temp = temp->next;
  20. }
  21. std::cout << std::endl;
  22. }
  23. int *array() {
  24. int *array = new int[len()];
  25. ListNode *temp = this;
  26. int index = 0;
  27. while (temp) {
  28. array[index++] = temp->val;
  29. temp = temp->next;
  30. }
  31. return array;
  32. }
  33. int len() {
  34. int length = 0;
  35. ListNode *temp = this;
  36. while (temp) {
  37. temp = temp->next;
  38. length++;
  39. }
  40. return length;
  41. }
  42. };
  43. ListNode *createRandomList(int len) {
  44. ListNode *p = nullptr;
  45. std::random_device rd;
  46. std::uniform_int_distribution<int> dis(1, 100);
  47. std::mt19937 gen(rd());
  48. while (--len >= 0) {
  49. auto *temp = new ListNode(dis(gen), p);
  50. p = temp;
  51. }
  52. return p;
  53. }
  54. #endif //LEECODE_C_LISTS_H
备用站点 当前处于降级运行的备用站点,仅供应急访问,数据和功能可能不是最新。