LeetCode开心刷题十三天——24

系统管理员 2023-08-17 15:14 188阅读 0赞

习惯就是人生的最大指导

——休谟

  1. Swap Nodes in Pairs

Medium

1209107FavoriteShare

Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list’s nodes, only nodes itself may be changed.

防止用增加值,存储前后变量只进行值交换,而不处理节点顺序指针的投机行为

Example:

  1. Given 1->2->3->4, you should return the list as 2->1->4->3.
  2. 额外收获:
  3. 每次刷题都会发现自己一些基本的概念没有弄清,&是其中之一
  4. &特别容易带给我的误解是取地址符号,其实在某些情况&表示是别名
  5. eg:int a; int &ra=a; //定义引用ra,它是变量a的引用,即别名
  6. 说人话:int 本小可爱;int &小仙女=本小可爱;
  7. 本小可爱其实就是小仙女,这是一个人,但是名字只能由一个所以平时大家叫我小可爱,但如果叫小仙女也是说我,而且因为
  8. 本身就是一个变量(人),所以操作都是共荣损的,有人给小仙女送花=给小可爱送花
  9. Opps,I suddenly notice I didn't write in English,My fault
  10. C11 is really a magically improve than C++ ,especially the auto.It changes this complex question
  11. into the simple one.It use a auxiliary variable to change the node of list
  12. #include <iostream>
  13. #include<vector>
  14. #include<iostream>
  15. #include<string>
  16. #include<stdio.h>
  17. #include<string.h>
  18. #include<iomanip>
  19. #include<vector>
  20. #include<list>
  21. #include<queue>
  22. #include<algorithm>
  23. #include<stack>
  24. #include<map>
  25. using namespace std;
  26. struct ListNode {
  27. int val;
  28. ListNode *next;
  29. ListNode(int x) : val(x), next(NULL) {}
  30. };
  31. class Solution {
  32. public:
  33. ListNode* swapPairs(ListNode* head) {
  34. if(!head||!head->next) return head;
  35. ListNode d(0);
  36. d.next=head;
  37. head=&d;
  38. //if we want some conditions exist directly use it,if not use !
  39. while(head&&head->next&&head->next->next)
  40. {
  41. auto n1=head->next;
  42. auto n2=n1->next;
  43. n1->next=n2->next;
  44. n2->next=n1;
  45. head->next=n2;
  46. head=n1;
  47. }
  48. return d.next;
  49. }
  50. };
  51. int main()
  52. {
  53. ListNode a(10);
  54. ListNode b(20);
  55. ListNode c(30);
  56. ListNode d(70);
  57. Solution s;
  58. a.next=&b;
  59. b.next=&c;
  60. c.next=&d;
  61. ListNode *head=&a;
  62. // while(head)
  63. // {
  64. // cout<<head->val<<endl;
  65. // head=head->next;
  66. // }
  67. ListNode* res=NULL;
  68. res=s.swapPairs(head);
  69. while(res)
  70. {
  71. cout<<res->val<<endl;
  72. res=res->next;
  73. }
  74. return 0;
  75. }

我必须要用中文表达下我的兴奋之情:两个方面,学习上和心理上

学习:

我很久之情一直不懂引用具体的含义,有幸今天在这里面用了很多,搞懂了我之前认为的大boss问题 ListNode *p=&a;类似这样是指ListNode类型的指针变量指向a所在的地址,我们知道,指针是按地址来进行索引的。&这里是取地址,把a所在的地址赋值p。并不是引用,引用也是一种我不熟练的知识

知道了如何构建ListNode链表结构,上下节点之间该怎么连接,怎样输出链表

膜拜C11的神奇性能,auto变量简直无敌,给交换提供了难以想象的方便

可以进步的地方:

把ListNode值改成可以输入的

对比C++ python感受

心态:

我最近特别希望进入努力的状态,真的每天都有新的感悟和收获。昨天的感悟是与其抱怨不如行动,因为昨天抱怨了很久,所以晚上睡得晚,我就想要把这时间用来学习多好,今天则是很紧张,其实这是不可避免的,因为我在强迫自己提速,我要求自己快速做事,动作太快人自然就会焦虑,我一焦虑睡眠就差强人意,我今天已经很晚了还在学,又烦躁又焦虑就开始看搞笑视频,看到最后我想倒头就睡但是发现睡不着,胃也不舒服,情绪不要太亢奋,最好能保持一个比较平静的阶段是比较有利的,但是我最后还是拿起了电脑并告诉我自己无论再烦,无论无论再烦再烦,都必须写下去,写了,其实,人才能真正的宁静和放松下来,夜很安静了,只有我的敲键盘声,我不喜欢用睡美容觉的时间熬夜自虐,我只是希望在昨天“抱怨的时间不如去努力”的认知基础上,让自己切肤之痛的领悟到“焦虑不是逃避任务的借口” 无论怎么用搞笑视频逃避,最终都逃不过,都必须再回到这来,回来受这一场磨难,所以早完早好,自己要认清

转载于:https://www.cnblogs.com/Marigolci/p/11144895.html

发表评论

表情:
评论列表 (有 0 条评论,188人围观)

还没有评论,来说两句吧...

相关阅读