首页 > 其他分享 >LeetCode 206 反转链表,LeetCode 92反转链表Ⅱ

LeetCode 206 反转链表,LeetCode 92反转链表Ⅱ

时间:2023-08-05 18:34:59浏览次数:52  
标签:pre head ListNode cur 反转 next 链表 LeetCode

  1. 反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
示例 2:
输入:head = [1,2]
输出:[2,1]
示例 3:
输入:head = []
输出:[]

提示:
链表中节点的数目范围是 [0, 5000]
-5000 <= Node.val <= 5000
写法一:不使用头节点,可以看到需要三个变量

class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) return null;
        ListNode pre = null, cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}

写法二:使用头节点,你可以发现用了四个变量,因为你需要使用dummy.next来返回

class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) return null;
        ListNode dummy = new ListNode(0, head);
        ListNode pre = dummy, cur = pre.next;
        while (cur != null && cur.next != null) {
            ListNode next = cur.next;
            cur.next = cur.next.next;
            next.next = pre.next;
            pre.next = next;
        }
        return dummy.next;
    }
}

写法三:使用递归的方法,最简洁,但需要思考

class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode re = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return re;
    }
}
  1. 反转链表 II

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。

示例 1:

输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]
示例 2:

输入:head = [5], left = 1, right = 1
输出:[5]

提示:

链表中节点数目为 n
1 <= n <= 500
-500 <= Node.val <= 500
1 <= left <= right <= n

写法一:使用头节点,可以看出来很简单

class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode pre = new ListNode(0, head);
        ListNode dummy = pre;
        int cnt = right - left;
        while (left-- > 1) {
            pre = pre.next;
        }
        ListNode cur = pre.next;
        for (int i = 0; i < cnt; i++) {
            ListNode next = cur.next;
            cur.next = cur.next.next;
            next.next = pre.next;
            pre.next = next;
        }
        return dummy.next;
    }
}

写法二:对反转链表的不使用头节点有很深的理解,我最开始就是用的这种

class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode start = head, end = head;
        ListNode prefix = new ListNode(0, head);
        int temp = left;
        while (left-- > 1) {
            prefix = prefix.next;
            start = start.next;
        }
        while (right-- > 1) {
            end = end.next;
        }
        ListNode last = end.next;
        ListNode pre = last;
        while (start != last) {
            ListNode next = start.next;
            start.next = pre;
            pre = start;
            start = next;
        }
        if (temp == 1) return pre;
        prefix.next = pre;
        return head;
    }
}

标签:pre,head,ListNode,cur,反转,next,链表,LeetCode
From: https://www.cnblogs.com/xiaoovo/p/17608375.html

相关文章

  • LeetCode -- 722. 删除注释
     利用双指针来进行删除操作 classSolution{public:vector<string>removeComments(vector<string>&source){stringstr;for(autoit:source)str+=it+"'";intn=str.size();vector<string&g......
  • 洛谷 P1553 数字反转(升级版)
    题目描述给定一个数,请将该数各个位上数字反转得到一个新数。整数反转是将所有数位对调。小数反转是把整数部分的数反转,再将小数部分的数反转,不交换整数部分与小数部分。分数反转是把分母的数反转,再把分子的数反转,不交换分子与分母。百分数的分子一定是整数,百分数只改变数字......
  • #yyds干货盘点# LeetCode程序员面试金典:课程表
    题目:你这个学期必须选修numCourses门课程,记为 0 到 numCourses-1。在选修某些课程之前需要一些先修课程。先修课程按数组 prerequisites给出,其中 prerequisites[i]=[ai,bi],表示如果要学习课程 ai则必须先学习课程 bi。例如,先修课程对 [0,1]表示:想要学习......
  • #yyds干货盘点# LeetCode程序员面试金典:统计各位数字都不同的数字个数
    1.简述:给你一个整数n,统计并返回各位数字都不同的数字x的个数,其中0<=x<10n 。 示例1:输入:n=2输出:91解释:答案应为除去11、22、33、44、55、66、77、88、99外,在0≤x<100范围内的所有数字。示例2:输入:n=0输出:12.代码实现:classSolution{publicintcount......
  • LeetCode 3. 无重复字符的最长子串
    classSolution{public:intres=0;intlengthOfLongestSubstring(strings){intn=s.size();if(!n)return0;boolst[128]={false};for(intj=0,i=0;j<n;j++){while(j&&st[s[j]]==true......
  • 算法:深挖合并 K 个有序链表
    本人刷题时思考的几个解法,欢迎交流力扣链接:合并2个有序链表力扣链接:合并K个有序链表目录合并2个有序链表合并k个有序链表K个中minNode排序队列取minNode队头手动实现的排序队列优先级队列分治合并2个有序链表合并2个有序链表操作模型:for->cur=min(list1......
  • GO:用链表实现栈的操作
         ......
  • 反转链表系列图解
    1.反转链表给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。图解代码实现publicListNodeReverseList(ListNodehead){ListNodecur=head;ListNodepre=null;ListNodenext=null;......
  • 【数据结构 | 入门】线性表与链表 (问题引入&实现&算法优化)
    ......
  • LeetCode 739. 每日温度
    classSolution{public:vector<int>dailyTemperatures(vector<int>&t){stack<int>st;intn=t.size();vector<int>res(n);for(inti=n-1;i>=0;i--){while(st.size()&am......