zoukankan      html  css  js  c++  java
  • 单链表练习

    打印,反转之类

    import java.util.Stack;
    
    public class LinkedListReverse {
        public static void main(String[] args) {
            ListNode head = initialList();
            printSelfReverse (head);
        }
        /**原地反转操作 
         *                   head     head.next
         *            p1       p2       p3
         * node1 -> node2 -> node3 -> node4 -> node5
         * node1 <- node2 <- node3 <- node4 <- node5
         * */
        public static void printSelfReverse (ListNode head) {
            ListNode p1, p2;
            p1 = head;
            p2 = head.next;
            head = head.next;
            p1.setNext(null);
            while (head != null && head.next != null) {
                head = head.next;
                p2.setNext(p1);
                p1 = p2;
                p2 = head;
            }
            p2.setNext(p1);
            printList(p2);
        }
        //新建结点
        public static void printReverse (ListNode head) {
            ListNode p1, p2;
            p1 = new ListNode(head.val, null);
            p2 = new ListNode(head.next.val, null);
            head = head.next;
            while (head != null && head.next != null) {
                head = head.next;
                p2.setNext(p1);
                p1 = p2;
                p2 = new ListNode(head.val, null);
            }
            p2.setNext(p1);
            printList(p2);
        }
        //递归反向打印
        public static void printRecLinkedList(ListNode head){
            if (head != null && head.next != null) {
                printRecLinkedList(head.next);
            }
            System.out.println(head.val);
        }
        /**
         * node1 -> node2 -> node3 -> node4 -> node5
         * node1 <- node2 <- node3 <- node4 <- node5
         * */
        //递归反转
        public static void buildRecLinkedList(ListNode head){
            ListNode temp = new ListNode(head.val, null);
            while (head != null && head.next != null) {
                head = head.next;
                ListNode tt = new ListNode(head.val, null);
                tt.setNext(temp);
                temp = tt;
            }
            printList(temp);
        }
        //入栈出栈反向打印
        public static void printWhileLinkedList(ListNode head){
            Stack<ListNode> st = new Stack<ListNode>(); 
            st.push(head);
            while (head != null && head.next != null) {
                head = head.next;
                st.push(head);
            }
    
            while (!st.isEmpty()) {
                System.out.println(st.pop().val);
            }
        }
        //初始化一个链表
        private static ListNode initialList() {
            ListNode head = new ListNode(1, null);
            ListNode node1 = new ListNode(2, null);
            ListNode node2 = new ListNode(3, null);
            ListNode node3 = new ListNode(4, null);
            ListNode node4 = new ListNode(5, null);
            ListNode node5 = new ListNode(6, null);
            head.setNext(node1);
            node1.setNext(node2);
            node2.setNext(node3);
            node3.setNext(node4);
            node4.setNext(node5);
            return head;
        }
        //打印list
        private static void printList(ListNode temp) {
            System.out.println(temp.val);
            while ((temp = temp.next) != null) {
                System.out.println(temp.val);
            }
        }
    }
    class ListNode{
        int val;
        ListNode next;
    
        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
        public void setNext(ListNode next) {
            this.next = next;
        }
    }
  • 相关阅读:
    OGG实时同步Oracle数据到Kafka实施文档(供flink流式计算)
    Oracle exp导出加where指定条件
    oracle merge into的用法
    Oracle列转行函数LISTAGG() WITHIN GROUP ()的使用方法
    sql怎样查一个存储过程被谁调用
    Oracle JOB间隔时间详解
    如何在ORACLE下创建JOB,并且赋予ID号?
    DOS下查看进程对应的文件路径
    查询系统中运行的JOB
    plsql中书写一个简单的存储过程
  • 原文地址:https://www.cnblogs.com/it-worker365/p/6956656.html
Copyright © 2011-2022 走看看