链表的操作-反转链表

链表

160相交链表

代码

cpp 复制代码
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
            ListNode* h1=headA;
            ListNode* h2=headB;
            while(h1&&h2)
            {
                if(h1!=h2)
                {
                    h1=h1->next;
                    h2=h2->next;
                }else{
                    return h1;
                }
            }
            if(h1==nullptr)
            {
                h1=headB;
            }else{
                h2=headA;
            }
            while(h1&&h2)
            {
                if(h1!=h2)
                {
                    h1=h1->next;
                    h2=h2->next;
                }else{
                    return h1;
                }
            }
            if(h1==nullptr)
            {
                h1=headB;
            }else{
                h2=headA;
            }
            while(h1&&h2)
            {
                if(h1!=h2)
                {
                    h1=h1->next;
                    h2=h2->next;
                }else{
                    return h1;
                }
            }
            return nullptr;
    }
};

代码优化

通过两次遍历,headA,headB走的距离一定是一样的。

最终只会出现两种情况:

  1. 相等指向同一个ListNode,

  2. 两个链表中没有相交的点,又因为最后指向的都是空指针

    ListNode *getIntersectionNode(ListNode headA, ListNode headB) {
    ListNode
    h1=headA;
    ListNode
    h2=headB;
    while(h1!=h2)
    {
    h1=(h1==nullptr)?headB:h1->next;
    h2=(h2==nullptr)?headA:h2->next;
    }
    return h1;
    }

其他方法

  1. 记录长度,这个办法好像没办法解决,因为你不知道长度在那?
  2. 通过map,set存放ListNode,然后边遍历,边计算是否存在

反转链表

  1. 方法1:
cpp 复制代码
    ListNode* reverseList(ListNode* head) {
       ListNode* pre=nullptr;
       ListNode* cur=head;
       while(cur)
       {
         ListNode* ne=cur->next;
         cur->next=pre;
         pre=cur;
         cur=ne;
       }
       return pre;
    }

方法2:

cpp 复制代码
 ListNode* reverseList(ListNode* head) {
       ListNode* pre=new ListNode(-1);
       ListNode* cur=head;
       while(cur)
       {
         ListNode* ne=cur->next;
         cur->next=pre->next;
         pre->next=cur;
         cur=ne;
       }
       ListNode* t=pre->next;
       delete pre;
       return t;
    }

递归的方法

cpp 复制代码
ListNode* reverseList(ListNode* head) {
       if(head==nullptr||head->next==nullptr)
       {
         return head;
       }
       ListNode* last=reverseList(head->next);
       head->next->next=head;
       head->next=nullptr;
       return last;
       
    }
e* last=reverseList(head->next);
       head->next->next=head;
       head->next=nullptr;
       return last;
       
    }
相关推荐
xsyaaaan4 分钟前
leetcode-hot100-二叉树
数据结构·leetcode
xiaoye-duck23 分钟前
《算法题讲解指南:优选算法-哈希表》--58.存在重复元素I,59.存在重复元素II,60.字母异位词分组
数据结构·c++·哈希算法
SadSunset1 小时前
第四章:Redis 数据结构与命令
数据结构·数据库·redis
北顾笙9801 小时前
day09-数据结构力扣
数据结构·算法·leetcode
小捏哩2 小时前
死锁检测组件的设计
linux·网络·数据结构·c++·后端
左左右右左右摇晃2 小时前
Java线程池工作原理与回收机制
java·jvm·数据结构
承渊政道2 小时前
【优选算法】(实战领略前缀和的真谛)
开发语言·数据结构·c++·笔记·学习·算法
handsomethefirst2 小时前
【算法与数据结构】【面试经典150题】【题46-题50】
数据结构·算法·面试
大黄说说2 小时前
PHP 数组 vs SPL 数据结构:队列与栈场景下的性能对决
开发语言·数据结构·php
宵时待雨3 小时前
C++笔记归纳15:封装map & set
开发语言·数据结构·c++·笔记·算法