后端秋招上岸大厂刷题第二天(链表)高频

最困难的事情就是认识自己。–希腊


一、从尾到头打印链表

问题描述

在这里插入图片描述

方法一:原地翻转

思路分析

  1. 首先创建两个节点prev current,一个初始化为空,另一个是指向head,这两节点的作用为保存前一个节点和当前节点
  2. 通过循环,改变节点的指向,改变指向的操作如下:首先创建一个节点来保存下一个节点,将当前节点的next指向前一个节点prev,由于新节点的缘故,current后面部分的链表并没有丢失,只需要将前一个节点prev指针变成current,再将当前节点指针改变成新节点即可(current=newNode),类似递归思想,最后的结束条件是current==null,表示后面没有节点了
  3. 最后循环遍历,加入到数组中,思想就是这样,在改变指向的操作中每个人的操作顺序不一样,遍历时候采用开始节点也就不一样,比如我在while中使用的是current.next!=null 的话,length和下面就不能使用prev了,

代码实现

java版本

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        //Reverse
        ListNode prev = null;
        ListNode current = head;
        int length = 0;
        while(current != null) {
            ListNode newNode = current.next;
            current.next = prev;
            prev = current;
            current = newNode;
            length = length + 1;
        }
        int[] result = new int[length];
       for (int i = 0; i < result.length; i++) {
            result[i] = prev.val;
            prev = prev.next;
        }

        return result;
    }
}

方法二:辅助栈

这个思路就很清晰了,先进后出,刚好能够满足我们链表的反转
代码如下:

class Solution {
    public int[] reversePrint(ListNode head) {
        LinkedList<Integer> stack = new LinkedList<Integer>();
        while(head != null) {
            stack.addLast(head.val);
            head = head.next;
        }
        int[] res = new int[stack.size()];
        for(int i = 0; i < res.length; i++)
            res[i] = stack.removeLast();
    return res;
    }
}

二、反转链表

问题描述:

在这里插入图片描述

代码实现:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode current = head;
        while(current!=null){
            ListNode newNode = current.next;
            current.next = prev;
            prev = current;
            current = newNode;
        }
        return prev;
    }
}

运行截图

在这里插入图片描述

三、复杂链表的复制

题目描述:

在这里插入图片描述
在这里插入图片描述

思路分析:

赖皮写法直接上代码

代码实现:

class Solution {
    public Node copyRandomList(Node head) {
        if(head == null) return null;
        Node cur = head;
        Map<Node, Node> map = new HashMap<>();
        // 3. 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射
        while(cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        // 4. 构建新链表的 next 和 random 指向
        while(cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        // 5. 返回新链表的头节点
        return map.get(head);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

取酒鱼食--【余九】

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值