【链表算法基础】实现反转链表以及基于链表实现队列和栈

简述

最近还是继续在学习左神的算法班,然后刚好学到了链表基础,顺便贴一下自己的代码,当前主要是几道基础题

实现反转链表

package ListText;

/**
 * 链表题目1:写出反转链表的函数
 */
public class ListReverse {

    // 内部class类
    public static class Node<V> {
        public V value;
        public Node<V> next;

        public Node(V v) {
            this.value = v;
        }
    }

    // 测试反转链表的函数
    public static void main(String[] args) {
        //1、首先先创建链表
        Node<Integer> node1 = new Node<>(1);
        node1.next = new Node<>(2);
        node1.next.next = new Node<>(3);
        Node<Integer> nodeTemp = node1;
        /*while (nodeTemp != null) {
            System.out.print(nodeTemp.value + " ");
            nodeTemp = nodeTemp.next;
        }*/
        nodeTemp = reverseList(node1);
        while (nodeTemp != null) {
            System.out.print(nodeTemp.value + " ");
            nodeTemp = nodeTemp.next;
        }
    }

    /**
     * 反转链表的函数,主要思路为:
     *  A -> B -> C 转为 A <- B <- C,然后返回C node,因为需要有变量挂载着
     *  1、创建指针pre、next、head
     *  2、如以A为例,先获取next指向B
     *  3、A.next 指向 pre
     *  4、pre = head; head = next; next = head.next
     *  5、一直循环到head为null,然后返回最后一个pre
     */
    public static Node<Integer> reverseList(Node head) {
        Node<Integer> pre = null;
        Node<Integer> next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

}

基于单向链表实现队列和栈

package ListText;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class ListToQueueAndStack {

    public static class Node<V> {
        public V value;
        public Node<V> next;

        public Node(V v) {
            this.value = v;
        }
    }

    /**
     * 使用链表实现队列类,支持方法为
     * 1.isEmpty() 判断是否为空
     * 2.size()  当前队列的长度
     * 3.offer(V value) 新增节点
     * 4、poll() 推出节点
     * 5、peek() 下一个推出的节点内容
     */
    public static class MyQueue<V> {
        // 因为队列是先入先出,所以在出的时候需要从head弹出,入的时候需要从tail加入
        public Node<V> head;
        public Node<V> tail;
        // 还有要判断为空和获取长度,需要有size
        public int size;

        public MyQueue() {
            head = null;
            tail = null;
            size = 0;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        public int size() {
            return size;
        }

        public void offer(V value) {
            Node<V> node = new Node<>(value);
            if (isEmpty()) {
                head = node;
            } else {
                tail.next = node;
            }
            tail = node;
            size++;
        }

        public V poll() {
            V value = null;
            if (head != null) {
                value = head.value;
                head = head.next;
            } else {
                tail = null;
            }
            size--;
            return value;

        }

        public V peek() {
            if (head != null) {
                return head.value;
            }
            return null;
        }
    }

    /**
     * 使用链表实现队列类,支持方法为
     * 1.isEmpty() 判断是否为空
     * 2.size()  当前队列的长度
     * 3.push(V value) 压入一个元素在栈中
     * 4.pop() 从栈中推出元素
     * 5、peek() 下一个推出的元素
     */
    public static class MyStack<V> {
        // 思路,只需要head即可,因为栈是后入后出,所以只需要一个head节点,在压栈的时候,新的节点为head节点指向旧节点,此时推出的时候直接返回head节点即可,因为后入后出
        public Node<V> head;
        public int size;


        public MyStack() {
            head = null;
            size = 0;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        public int size() {
            return size;
        }

        public void push(V value) {
            Node<V> node = new Node<>(value);
            if (head != null) {
                node.next = head;
            }
            head = node;
            size++;
        }

        public V pop() {
            V ans = null;
            if (head != null) {
                ans = head.value;
                head = head.next;
                size--;
            }
            return ans;
        }

        public V peek() {
            V ans = null;
            if (head != null) {
                ans = head.value;
            }
            return ans;
        }
    }

    public static void testQueue() {
        MyQueue<Integer> myQueue = new MyQueue<>();
        Queue<Integer> test = new LinkedList<>();
        int testTime = 5000000;
        int maxValue = 200000000;
        System.out.println("测试开始!");
        for (int i = 0; i < testTime; i++) {
            if (myQueue.isEmpty() != test.isEmpty()) {
                System.out.println("Oops!");
            }
            if (myQueue.size() != test.size()) {
                System.out.println("Oops!");
            }
            double decide = Math.random();
            if (decide < 0.33) {
                int num = (int) (Math.random() * maxValue);
                myQueue.offer(num);
                test.offer(num);
            } else if (decide < 0.66) {
                if (!myQueue.isEmpty()) {
                    int num1 = myQueue.poll();
                    int num2 = test.poll();
                    if (num1 != num2) {
                        System.out.println("Oops!");
                    }
                }
            } else {
                if (!myQueue.isEmpty()) {
                    int num1 = myQueue.peek();
                    int num2 = test.peek();
                    if (num1 != num2) {
                        System.out.println("Oops!");
                    }
                }
            }
        }
        if (myQueue.size() != test.size()) {
            System.out.println("Oops!");
        }
        while (!myQueue.isEmpty()) {
            int num1 = myQueue.poll();
            int num2 = test.poll();
            if (num1 != num2) {
                System.out.println("Oops!");
            }
        }
        System.out.println("测试结束!");
    }

    public static void testStack() {
        MyStack<Integer> myStack = new MyStack<>();
        Stack<Integer> test = new Stack<>();
        int testTime = 5000000;
        int maxValue = 200000000;
        System.out.println("测试开始!");
        for (int i = 0; i < testTime; i++) {
            if (myStack.isEmpty() != test.isEmpty()) {
                System.out.println("Oops!");
            }
            if (myStack.size() != test.size()) {
                System.out.println("Oops!");
            }
            double decide = Math.random();
            if (decide < 0.33) {
                int num = (int) (Math.random() * maxValue);
                myStack.push(num);
                test.push(num);
            } else if (decide < 0.66) {
                if (!myStack.isEmpty()) {
                    int num1 = myStack.pop();
                    int num2 = test.pop();
                    if (num1 != num2) {
                        System.out.println("Oops!");
                    }
                }
            } else {
                if (!myStack.isEmpty()) {
                    int num1 = myStack.peek();
                    int num2 = test.peek();
                    if (num1 != num2) {
                        System.out.println("Oops!");
                    }
                }
            }
        }
        if (myStack.size() != test.size()) {
            System.out.println("Oops!");
        }
        while (!myStack.isEmpty()) {
            int num1 = myStack.pop();
            int num2 = test.pop();
            if (num1 != num2) {
                System.out.println("Oops!");
            }
        }
        System.out.println("测试结束!");
    }

    public static void main(String[] args) {
        testQueue();
        testStack();
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值