链表的操作-反转链表

链表

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;
       
    }
相关推荐
范纹杉想快点毕业1 天前
STM32单片机与ZYNQ PS端 中断+状态机+FIFO 综合应用实战文档(初学者版)
linux·数据结构·数据库·算法·mongodb
多米Domi0111 天前
0x3f 第49天 面向实习的八股背诵第六天 过了一遍JVM的知识点,看了相关视频讲解JVM内存,垃圾清理,买了plus,稍微看了点确定一下方向
jvm·数据结构·python·算法·leetcode
L_09071 天前
【C++】高阶数据结构 -- 红黑树
数据结构·c++
划破黑暗的第一缕曙光2 天前
[数据结构]:5.二叉树链式结构的实现1
数据结构
青桔柠薯片2 天前
数据结构:单向链表,顺序栈和链式栈
数据结构·链表
XiaoFan0122 天前
将有向工作流图转为结构树的实现
java·数据结构·决策树
睡一觉就好了。2 天前
快速排序——霍尔排序,前后指针排序,非递归排序
数据结构·算法·排序算法
齐落山大勇2 天前
数据结构——单链表
数据结构
皮皮哎哟2 天前
深入浅出双向链表与Linux内核链表 附数组链表核心区别解析
c语言·数据结构·内核链表·双向链表·循环链表·数组和链表的区别
wWYy.2 天前
指针与引用区别
数据结构