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;
    }
}

总结:

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

你可能感兴趣的:(leetcode,leetcode,java)