单链表练习

打印,反转之类

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;
    }
}
原文地址:https://www.cnblogs.com/it-worker365/p/6956656.html