链表的操作-反转链表

链表

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;
       
    }
相关推荐
Fanxt_Ja3 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
今后1233 天前
【数据结构】二叉树的概念
数据结构·二叉树
散1123 天前
01数据结构-01背包问题
数据结构
消失的旧时光-19433 天前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Gu_shiwww3 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
苏小瀚3 天前
[数据结构] 排序
数据结构
_不会dp不改名_3 天前
leetcode_21 合并两个有序链表
算法·leetcode·链表
睡不醒的kun4 天前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌4 天前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
Whisper_long4 天前
【数据结构】深入理解堆:概念、应用与实现
数据结构