Leetcode剑指Offer刷题 - 第二天

 Leetcode剑指Offer刷题指南:

Leetcode剑指Offer刷题-学习计划目录_DEGv587的博客-CSDN博客

剑指 Offer 06. 从尾到头打印链表

解法一:辅助栈

class Solution {
    public int[] reversePrint(ListNode head) {
        //辅助栈
        Deque<Integer> stack = new ArrayDeque<>();
        int count = 0;
        while (head != null) {
            stack.push(head.val);
            count++;
            head = head.next;
        }
        //辅助数组
        int[] ret = new int[count];
        for (int i = 0; i < count; ++i) {
            ret[i] = stack.pop();
        }
        return ret;
    }
}

解法二:翻转链表

1.(带傀儡节点)

class Solution {
    public int[] reversePrint(ListNode head) {
        //翻转链表
        ListNode newHead = null;
        int count = 0;
        while (head != null) {
            ListNode tmp = head.next;
            head.next = newHead;
            newHead = head;
            head = tmp;
            count++;
        }

        int[] ret = new int[count];
        int i = 0;
        while (newHead != null) {
            ret[i++] = newHead.val;
            newHead = newHead.next;
        }
        return ret;
    }
}

2.(不带傀儡节点)

class Solution {
    public int[] reversePrint(ListNode head) {
        //翻转链表(不带傀儡节点)
        ListNode pre = null;
        ListNode cur = head;
        ListNode curNext = null;
        int count = 0;
        while (cur != null) {
            curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
            count++;
        }

        int[] ret = new int[count];
        int i = 0;
        while (pre != null) {
            ret[i++] = pre.val;
            pre = pre.next;
        }
        return ret;
    }
}

解法三:反向填充数据:计数器 + 从后向前遍历

class Solution {
    public int[] reversePrint(ListNode head) {
        ListNode node = head;
        int count = 0;
        //计数
        while (node != null) {
            ++count;
            node = node.next;
        }

        int[] ret = new int[count];
        node = head;
        //从后向前遍历,依次放入
        for (int i = count - 1; i >= 0; --i) {
            ret[i] = node.val;
            node = node.next;
        }

        return ret;
    }
}

剑指 Offer 24. 反转链表

解法一:反转链表(同上题法2中两种)

解法二:递归

class Solution {
    public ListNode reverseList(ListNode head) {
        //递归
        if (head == null || head.next == null) {
            return head;
        }
        ListNode node = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return node;
    }
}

剑指 Offer 35. 复杂链表的复制

解法一:回溯 + 哈希表

class Solution {
    Map<Node, Node> map = new HashMap<Node, Node>();

    public Node copyRandomList(Node head) {
        //回溯 + 哈希表
        if(head == null) {
            return null;
        }

        if (!map.containsKey(head)) {
            Node newHead = new Node(head.val);
            map.put(head, newHead);
            newHead.next = copyRandomList(head.next);
            newHead.random = copyRandomList(head.random);
        }

        return map.get(head);
    }
}

解法二:迭代 + 节点拆分

1.复制值相同,没有分配next和random的node

2.将两条链连接起来,复制random,将 node的random.next 复制给 node.next.random

3.将两条链断开,node.next -> node.next.next

class Solution {
    //迭代 + 节点拆分
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }

        for (Node node = head; node != null; node = node.next.next) {
            Node newNode = new Node(node.val);
            newNode.next = node.next;
            node.next = newNode;
        }

        for (Node node = head; node != null; node = node.next.next) {
            Node newNode = node.next;
            newNode.random = (node.random == null) ? null : node.random.next;
        }

        Node newHead = head.next;
        for (Node node = head; node != null; node = node.next) {
            Node newNode = node.next;
            node.next = node.next.next;
            newNode.next = (newNode.next == null) ? null : newNode.next.next;
        }

        return newHead;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值