链表的操作-反转链表

链表

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;
       
    }
相关推荐
LYFlied44 分钟前
【每日算法】LeetCode 5. 最长回文子串(动态规划)
数据结构·算法·leetcode·职场和发展·动态规划
雪花desu1 小时前
【Hot100-Java中等】/LeetCode 128. 最长连续序列:如何打破排序思维,实现 O(N) 复杂度?
数据结构·算法·排序算法
程序员阿鹏2 小时前
如何保证写入Redis的数据不重复
java·开发语言·数据结构·数据库·redis·缓存
历程里程碑3 小时前
滑动窗口秒解LeetCode字母异位词
java·c语言·开发语言·数据结构·c++·算法·leetcode
Helibo443 小时前
2025年12月gesp3级题解
数据结构·c++·算法
靠沿3 小时前
Java数据结构初阶——堆与PriorityQueue
java·开发语言·数据结构
禾叙_3 小时前
HashMap
java·数据结构·哈希算法
zcbdandan4 小时前
JNA内存对齐导致的结构体数组传输错误
数据结构·算法
持梦远方5 小时前
持梦行文本编辑器(cmyfEdit):架构设计与十大核心功能实现详解
开发语言·数据结构·c++·算法·microsoft·visual studio
im_AMBER5 小时前
Leetcode 90 最佳观光组合
数据结构·c++·笔记·学习·算法·leetcode