算法4之链表

概述

单双链表的反转,单链表实现队列

单链表反转

链表节点的定义

java 复制代码
public class ListNode<V> {
    public V val;
    public ListNode<V> next;
    ListNode() {}
    ListNode(V val) { this.val = val; }
    ListNode(V val, ListNode<V> next) { this.val = val; this.next = next; }
}

单链表反转,leetcode: https://leetcode.cn/problems/reverse-linked-list/description/

java 复制代码
 /**
     * 单链表的反转
     * <a href="https://leetcode.cn/problems/reverse-linked-list/description/">...</a>
     * null-1-2-3-4-5
     * pre = null
     * head = 1
     * next = head.next
     * head.next = pre
     * pre = head
     * head = next
     * 先记住下一个值,然后改变指针方向。然后pre和head各流转到下一个值
     */
    public ListNode reverseList(ListNode head){
        if(head == null){
            return head;
        }
        ListNode pre = null;
        ListNode next = null;
        while(head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return head;
    }

双链表的反转

双链表节点的定义

java 复制代码
public class DoubleNode<V> {

    V val;
    DoubleNode<V> next;
    DoubleNode<V> last;
    DoubleNode() {}
    DoubleNode(V val) { this.val = val; }
    DoubleNode(V val, DoubleNode<V> next, DoubleNode<V> last) { this.val = val; this.next = next; this.last = last;}

}

双链表反转

java 复制代码
/**
     * 双链表的反转
     * -
     * 思路和单链表一样,只不过是多了一个指针。
     * 先记住下一个值,然后改变指针方向。然后pre和head各流转到下一个值
     */
    public DoubleNode reverseDoubleList(DoubleNode head){
        if(head == null){
            return head;
        }
        DoubleNode pre = null;
        DoubleNode next = null;
        while(head != null) {
            next = head.next;
            head.next = pre;
            head.last = next;
            pre = head;
            head = next;
        }
        return head;
    }

单链表实现队列

java 复制代码
class MyQueue<V>{
        // head记录队列的头节点
        private ListNode<V> head;
        // tail记录队列的尾节点
        private ListNode<V> tail;
        // 队列大小
        private int size;

        // 判空
        public boolean isEmpty(){
            return this.size == 0;
        }

        // 获取队列长度
        public int size(){
            return this.size;
        }

        /**
         * 元素压入队列
         * 更新head,tail,size
         * 思路:
         * 压入第一个元素,比如1,那么head=1,tail=1;
         * 压入第二个元素,比如2,那么1-2,即head=1, tail=2;
         * 压入第三个元素,比如3,那么1-2-3,即head=1, tail=3;
         * ...
         * 压入第i个元素,比如i,那么1-2-3...-i,即head=1,tail=i;
         * 每次压入元素时,原来的tail元素指向新压入的元素。即tail.next = cur;
         * tail都会更新,即tail = cur;
         * @param value 元素
         */
        public void offer(V value){
            ListNode<V> cur = new ListNode<>(value);
            if(tail == null){
                head = cur;
                tail = cur;
            }else{
                tail.next = cur;
                tail = cur;
            }
            size++;
        }

        /**
         * 元素弹出队列
         * 遵循队列,先进先出的特点,所以每次弹出的都是队列的head
         * 思路:
         * 如果队列不为空
         * 比如,当前队列是:1-2-3-4-5,head=1,tail=5;
         * 此时弹出,那么head会更新,head = head.next;
         *
         * 如果队列为空
         * 那么head = null; 此时注意tail要和head保持一致,否则会出现head=null,但是tail=5的情况
         */
        public V poll(){
            V res = null;
            if(head != null){
                res = head.val;
                head = head.next;
                size--;
            }else{
                tail = head;
            }
            return res;
        }

        // 返回队列头部元素
        public V peek(){
            if(head != null){
                return head.val;
            }
            return null;
        }

    }
相关推荐
2401_8812444031 分钟前
Treap树
数据结构·算法
乌萨奇也要立志学C++33 分钟前
二叉树OJ题(单值树、相同树、找子树、构建和遍历)
数据结构·算法
网安INF35 分钟前
深度学习中的逻辑回归:从原理到Python实现
人工智能·python·深度学习·算法·逻辑回归
wsxqaz1 小时前
浏览器原生控件上传PDF导致hash值不同
算法·pdf·哈希算法
NAGNIP1 小时前
Transformer注意力机制——MHA&MQA&GQA
人工智能·算法
摘星编程1 小时前
多模态AI Agent技术栈解析:视觉-语言-决策融合的算法原理与实践
人工智能·算法·多模态ai·视觉语言融合·ai决策算法
NAGNIP1 小时前
一文搞懂KV-Cache
人工智能·算法
CoovallyAIHub1 小时前
RTMPose:重新定义多人姿态估计的“实时”标准!
深度学习·算法·计算机视觉
爱喝茶的小茶1 小时前
周赛98补题
开发语言·c++·算法
小庞在加油2 小时前
《dlib库中的聚类》算法详解:从原理到实践
c++·算法·机器学习·数据挖掘·聚类