首页 > 编程语言 >代码随想录算法训练营第三天|链表理论基础 ,203.移除链表元素,707.设计链表,206.反转链表

代码随想录算法训练营第三天|链表理论基础 ,203.移除链表元素,707.设计链表,206.反转链表

时间:2023-01-13 23:33:30浏览次数:57  
标签:index cur nums 随想录 next 链表 移除 LinkedNode

一、参考资料

链表理论基础

文章链接:https://programmercarl.com/%E9%93%BE%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html

移除链表元素

题目链接/文章讲解/视频讲解::https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1%A8%E5%85%83%E7%B4%A0.html

设计链表

题目链接/文章讲解/视频讲解:https://programmercarl.com/0707.%E8%AE%BE%E8%AE%A1%E9%93%BE%E8%A1%A8.html

反转链表

题目链接/文章讲解/视频讲解:https://programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html

二、LeetCode203.移除链表元素

我的代码
  1. class Solution {
  2. public:
  3. vector<int> sortedSquares(vector<int>& nums) {
  4. vector<int> res;
  5. for (int i = 0; i < nums.size(); i++){
  6. nums[i] = nums[i] * nums[i];
  7. }
  8. sort(nums.begin(), nums.end());
  9. return nums;
  10. }
  11. };

接下来学习一下双指针的代码实现:

双指针实现
  1. class Solution {
  2. public:
  3. vector<int> sortedSquares(vector<int>& nums) {
  4. // 这里定义一个结果集,长度为原数组的大小
  5. vector<int> res(nums.size(), 0);
  6. // 定义首尾指针
  7. int firstp = 0;
  8. int lastp = nums.size() - 1;
  9. int k = lastp; // k表示结果集的指针,从后向前写入vector
  10. while (firstp <= lastp){
  11. if (nums[firstp] * nums[firstp] < nums[lastp] * nums[lastp]){
  12. res[k--] = nums[lastp] * nums[lastp];
  13. lastp--;
  14. }
  15. else{
  16. res[k--] = nums[firstp] * nums[firstp];
  17. firstp++;
  18. }
  19. }
  20. return res;
  21. }
  22. };

三、LeetCode707.设计链表

  1. class MyLinkedList {
  2. public:
  3. // 定义链表节点结构体——单链表
  4. struct LinkedNode {
  5. int val; // 节点上存储的元素
  6. LinkedNode *next; // 指向下一个节点的指针
  7. LinkedNode(int x) : val(x), next(NULL){} //节点的构造函数
  8. };
  9. // 初始化链表
  10. MyLinkedList() {
  11. _dummyHead = new LinkedNode(0); // 定义虚拟头结点,不是真正的链表头结点
  12. _size = 0;
  13. }
  14. // 获取第index个节点的数值,如果index是非法数值直接返回-1
  15. int get(int index) {
  16. if (index > (_size - 1) || index < 0) {
  17. return -1;
  18. }
  19. LinkedNode *cur = _dummyHead->next;
  20. // 如果--index就会陷入死循环
  21. while (index--) {
  22. cur = cur->next;
  23. }
  24. return cur->val;
  25. }
  26. // 在链表最前面插入一个节点,插入完成后,新插入的节点为链表的新的头结点
  27. void addAtHead(int val) {
  28. LinkedNode *newNode = new LinkedNode(val);
  29. newNode->next = _dummyHead->next;
  30. _dummyHead->next = newNode;
  31. _size++;
  32. }
  33. // 在链表最后面添加一个节点
  34. void addAtTail(int val) {
  35. LinkedNode *newNode = new LinkedNode(val);
  36. LinkedNode *cur = _dummyHead;
  37. while(cur->next != NULL) {
  38. cur = cur->next;
  39. }
  40. cur->next = newNode;
  41. _size++;
  42. }
  43. // 添加节点
  44. void addAtIndex(int index, int val) {
  45. if (index > _size) return;
  46. if (index < 0) index = 0;
  47. LinkedNode *newNode = new LinkedNode(val);
  48. LinkedNode *cur = _dummyHead;
  49. while (index--) {
  50. cur = cur->next;
  51. }
  52. newNode->next = cur->next;
  53. cur->next = newNode;
  54. _size++;
  55. }
  56. // 删除节点
  57. void deleteAtIndex(int index) {
  58. if (index >= _size || index < 0) {
  59. return;
  60. }
  61. LinkedNode *cur = _dummyHead;
  62. while (index--) {
  63. cur = cur->next;
  64. }
  65. LinkedNode *tmp = cur->next;
  66. cur->next = tmp->next; // cur->next = cur->next->next;
  67. delete tmp;
  68. _size--;
  69. }
  70. private:
  71. int _size;
  72. LinkedNode* _dummyHead;
  73. };
  74. // // 打印链表
  75. // void printLinkedList() {
  76. // LinkedNode* cur = _dummyHead;
  77. // while (cur->next != nullptr) {
  78. // cout << cur->next->val << " ";
  79. // cur = cur->next;
  80. // }
  81. // cout << endl;
  82. // }
  83. /**
  84. * Your MyLinkedList object will be instantiated and called as such:
  85. * MyLinkedList* obj = new MyLinkedList();
  86. * int param_1 = obj->get(index);
  87. * obj->addAtHead(val);
  88. * obj->addAtTail(val);
  89. * obj->addAtIndex(index,val);
  90. * obj->deleteAtIndex(index);
  91. */

四、LeetCode206.反转链表

这个题以前是做过的,链表用的不熟练还是不能很快写出来(这篇先写了双指针法,递归法下次补上)

双指针法实现反转链表
  1. class Solution {
  2. public:
  3. ListNode* reverseList(ListNode* head) {
  4. ListNode* temp; // 保存cur的下一个节点
  5. ListNode* cur = head;
  6. ListNode* pre = NULL;
  7. while(cur) {
  8. temp = cur->next; // 保存一下 cur的下一个节点,因为接下来要改变cur->next
  9. cur->next = pre; // 翻转操作
  10. // 更新pre 和 cur指针
  11. pre = cur;
  12. cur = temp;
  13. }
  14. return pre;
  15. }
  16. };
递归法(卡哥带注释版本)
  1. class Solution {
  2. public:
  3. ListNode* reverseList(ListNode* head) {
  4. // 边缘条件判断
  5. if(head == NULL) return NULL;
  6. if (head->next == NULL) return head;
  7. // 递归调用,翻转第二个节点开始往后的链表
  8. ListNode *last = reverseList(head->next);
  9. // 翻转头节点与第二个节点的指向
  10. head->next->next = head;
  11. // 此时的 head 节点为尾节点,next 需要指向 NULL
  12. head->next = NULL;
  13. return last;
  14. }
  15. };

Day03总结:

  1. 指针操作的原理还是可以理解的,但是写代码就不太顺手

  1. 看代码能看懂,但自己写的时候容易遗忘,或者思路容易不清楚

  1. 规范化操作很重要,包括对节点空间的释放(Java和python不需要考虑)以及结构体的初始化等。

标签:index,cur,nums,随想录,next,链表,移除,LinkedNode
From: https://www.cnblogs.com/ucaszym/p/17050977.html

相关文章