当前位置:   article > 正文

算法—链表操作

算法—链表操作

                 

        掌握数据结构链表的头插和尾插,删除,查找的操作,然后就可以根据题目要求实现其操作组合。

        对于链表来说,画图更好理解题解中的具体步骤,否则难以理解代码实际对数据结构的操作。

2. 两数相加 - 力扣(LeetCode)

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13. ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
  14. ListNode* new_list=new ListNode(0);
  15. ListNode* back=new_list;
  16. ListNode* cur1=l1; ListNode* cur2=l2;
  17. int t=0;
  18. while(cur1||cur2||t){
  19. if(cur1){
  20. t+=cur1->val;
  21. cur1=cur1->next;
  22. }
  23. if(cur2){
  24. t+=cur2->val;
  25. cur2=cur2->next;
  26. }
  27. int tmp=t%10;
  28. back->next=new ListNode(tmp);
  29. back=back->next;
  30. t=t/10;
  31. }
  32. ListNode* ret=new_list->next;
  33. delete new_list;
  34. return ret;
  35. }
  36. };

24. 两两交换链表中的节点 - 力扣(LeetCode)

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13. ListNode* swapPairs(ListNode* head) {
  14. if(!(head&&head->next)) return head;
  15. ListNode* new_list=new ListNode(0);
  16. new_list->next=head;
  17. ListNode* prev=new_list;
  18. ListNode* cur= head;
  19. ListNode* cur_n= head->next;
  20. ListNode* cur_n_n=cur_n->next;//记录将要移动到下次的位置
  21. while(cur&&cur_n){
  22. cur_n_n=cur_n->next;
  23. prev->next=cur_n;
  24. cur->next=cur_n_n;
  25. cur_n->next=cur;
  26. cout<<"cur:"<<cur->val<<"cur_n:"<<cur_n->val<<endl;;
  27. //下个位置
  28. prev=cur;//**1***cur位置移动到后面了
  29. cur=prev->next;
  30. if(cur)
  31. cur_n=cur->next;
  32. }
  33. ListNode* ret=new_list->next;
  34. delete new_list;
  35. return ret;
  36. }
  37. };

143. 重排链表 - 力扣(LeetCode)

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public://算法原理:将题目分解为两个0~n/2和n/2~n的链表,然后让后面的链表反转,依次交替插入。
  13. void reorderList(ListNode* head) {
  14. //从中间分解链表:左右指针
  15. ListNode* slow=head; ListNode* fast=head;
  16. while(fast->next&&fast->next->next){//fast->next=nullptr为结点数为偶数的链表,next->next为奇数
  17. slow=slow->next;
  18. fast=fast->next->next;
  19. }
  20. //分析后slow为0~n/2的链表的末结点
  21. ListNode* new_list=new ListNode;
  22. //反转0~n/2的链表
  23. ListNode *cur=slow->next;
  24. slow->next=nullptr;
  25. while(cur){
  26. ListNode* cur_n=cur->next;
  27. cur->next=new_list->next;
  28. new_list->next=cur;
  29. cur=cur_n;
  30. }
  31. //两链表交错链接,因为前面的操作0~n/2可能多一个点,所以cur2插入cur1后
  32. ListNode* cur1=head ; ListNode* cur2=new_list->next;
  33. while(cur1&&cur2){
  34. ListNode* cur1_n=cur1->next;
  35. ListNode* cur2_n=cur2->next;
  36. cur2->next=cur1->next;//cur2插入cur1后
  37. cur1->next=cur2;
  38. cur1=cur1_n;
  39. cur2=cur2_n;
  40. }
  41. return ;//不用返回
  42. }
  43. };

23. 合并 K 个升序链表 - 力扣(LeetCode)

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13. ListNode* mergeKLists(vector<ListNode*>& lists) {
  14. //归并排序->对于有效链表
  15. int n=lists.size();
  16. return mergesort(lists,0,n-1);//**1** n-1记得
  17. }
  18. ListNode* mergesort(vector<ListNode*>& lists,int left,int right){
  19. if(left==right) return lists[left];
  20. if(left>right) return nullptr;
  21. int mid=(left+right)/2;
  22. ListNode* list1=mergesort(lists,left,mid);
  23. ListNode* list2=mergesort(lists,mid+1,right);
  24. return merge_2List(list1,list2);
  25. }
  26. ListNode* merge_2List(ListNode* list1,ListNode* list2){
  27. ListNode * new_list = new ListNode;
  28. ListNode * back=new_list;
  29. ListNode *cur1=list1;
  30. ListNode *cur2=list2;
  31. while(cur1&&cur2){
  32. if(cur1->val <= cur2->val){
  33. back=back->next=cur1;
  34. cur1=cur1->next;
  35. }else{
  36. back=back->next=cur2;
  37. cur2=cur2->next;
  38. }
  39. }
  40. while(cur1){
  41. back=back->next=cur1;
  42. cur1=cur1->next;
  43. }
  44. while(cur2){
  45. back=back->next=cur2;
  46. cur2=cur2->next;
  47. }
  48. back= new_list->next;
  49. delete new_list;
  50. return back;
  51. }
  52. };

 25. K 个一组翻转链表 - 力扣(LeetCode)

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13. ListNode* reverseKGroup(ListNode* head, int k) {
  14. int n=0;
  15. ListNode *i=head;
  16. while(i){
  17. i=i->next;
  18. n++;
  19. }
  20. int len=n/k;//需要头插几次反转
  21. ListNode *new_list= new ListNode;
  22. ListNode* tmp=new_list;
  23. ListNode* cur=head;
  24. ListNode *next=nullptr;
  25. while(len--){
  26. ListNode* prev = tmp;
  27. tmp=cur;
  28. for(int i=0;i<k;i++){
  29. next=cur->next;
  30. cur->next=prev->next;
  31. prev->next=cur;
  32. cur=next;
  33. }
  34. }
  35. //链接末尾小于k不反转的结点
  36. tmp->next=next;//或者=cur,因为cur已经指向下一个结点
  37. tmp=new_list->next;
  38. delete new_list;
  39. return tmp;
  40. }
  41. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/460022
推荐阅读
相关标签
  

闽ICP备14008679号