链表的操作-反转链表

链表

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;
       
    }
相关推荐
爱装代码的小瓶子10 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
aramae13 小时前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法
cccc来财14 小时前
Java实现大根堆与小根堆详解
数据结构·算法·leetcode
刚入坑的新人编程16 小时前
暑期算法训练.9
数据结构·c++·算法·leetcode·面试·排序算法
找不到、了18 小时前
Java排序算法之<选择排序>
数据结构·算法·排序算法
小徐不徐说19 小时前
动态规划:从入门到精通
数据结构·c++·算法·leetcode·动态规划·代理模式
guguhaohao19 小时前
排序算法,咕咕咕
数据结构·算法·排序算法
晚云与城19 小时前
【数据结构】二叉树初阶详解(二):实现逻辑与代码拆解(超详版)
数据结构
小新学习屋19 小时前
《剑指offer》-数据结构篇-树
数据结构·算法·leetcode
此心安处是吾乡102419 小时前
数据结构 双向链表
数据结构·链表