leetcode92 翻转部分链表

题目:

leetcode92 题目链接:https://leetcode-cn.com/problems/reverse-linked-list-ii/

基础题一:
206 反转整个链表:https://leetcode-cn.com/problems/reverse-linked-list/
基础题二:
反转前 n 个链表

java代码实现:

基础题一代码:
非递归:

class Solution {
    public ListNode reverseList(ListNode head) {
        //判空
        if (head == null) return null;
        if (head.next == null) return head;
        ListNode fast = head, slow = head;
        //先走一步
        if (fast.next != null) {
            fast = fast.next;
            slow.next = null;
        }

        while(fast != null) {
            ListNode tmpNode= fast.next;
            fast.next = slow;
            slow = fast;
            fast = tmpNode;
        }
        return slow;
    }
}

非递归 头插法:

class Solution {
    public ListNode reverseList(ListNode head) {
        //头插法
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode fast = dummy.next, slow = dummy;
        while (fast != null && fast.next != null) {
            ListNode tmp = fast.next;
            fast.next = fast.next.next;
            tmp.next = slow.next;
            slow.next = tmp;
        }
        return dummy.next;
    }
}

非递归: labuladong大神解法(推荐):

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

递归:

class Solution {
    public ListNode reverseList(ListNode head) {
        //递归停止条件
        if (head == null || head.next == null) {
            return head;
        }
        ListNode cur = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return cur;
    }
}

基础题二代码:
递归:

class Solution {
ListNode successor = null; // 后驱节点

// 反转以 head 为起点的 n 个节点,返回新的头结点
ListNode reverseN(ListNode head, int n) {
    if (n == 1) {
        // 记录第 n + 1 个节点
        successor = head.next;
        return head;
    }
    // 以 head.next 为起点,需要反转前 n - 1 个节点
    ListNode last = reverseN(head.next, n - 1);

    head.next.next = head;
    // 让反转之后的 head 节点和后面的节点连起来
    head.next = successor;
    return last;
}
}

非递归:
略; 可用头插法;

翻转部分链表:
递归代码:

ListNode reverseBetween(ListNode head, int m, int n) {
    // base case
    if (m == 1) {
        return reverseN(head, n);
    }
    // 前进到反转的起点触发 base case
    head.next = reverseBetween(head.next, m - 1, n - 1);
    return head;
}

非递归代码:
即头插法:

class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        // 定义一个dummyHead, 方便处理
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;

        // 初始化指针
        ListNode g = dummyHead;
        ListNode p = dummyHead.next;

        // 将指针移到相应的位置
        for(int step = 0; step < m - 1; step++) {
            g = g.next; p = p.next;
        }

        // 头插法插入节点
        for (int i = 0; i < n - m; i++) {
            ListNode removed = p.next;
            p.next = p.next.next;

            removed.next = g.next;
            g.next = removed;
        }

        return dummyHead.next;
    }
}

总结:

翻转链表系列的题目,方法很多,非递归的方法中 头插法最好理解,效率最高;递归方法,代码精炼优雅,但理解比较难。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值