2-22逆序打印鏈表

題目描述

  • 給定一個鏈表,從尾到頭打印每一個元素。

解題方法1

  • 鏈表是鏈式存儲結構,不同於數組,沒有辦法直接逆序遍歷。
  • 第一反應,我們可以藉助於棧或數組,先順序遍歷鏈表把所有元素存儲到數組或棧中,然後再遍歷數組或棧進行逆序打印。
  • 空間複雜度爲n,時間複雜度爲n,不推薦。

public class Test {
    public static void main(String[] args) throws Exception {
        int[] arr = {10,20,30,40,50};
        Node head = create(arr);
        show(head);
    }
    //逆序打印
    public static void show(Node head){
        if(head==null){
            return;
        }
        Stack s = new Stack();
        //入棧
        for(Node p =head.next;p!=null;p=p.next){
            s.push(p.val);
        }
        //出棧
        while(!s.empty()){
            System.out.print(s.pop() + " ");
        }
    }

    //創建單鏈表
    public static Node create(int[] arr){
        Node head = new Node(0); //頭節點
        Node newnode = null; //指向新節點
        Node tail = head; //指向鏈表尾節點
        for(int a:arr){
            newnode = new Node(a);
            newnode.next = tail.next;
            tail.next = newnode;
            tail = newnode;
        }
        return head;
    }
}
class Node{
    int val;
    Node next;
    Node(int val){
        this.val = val;
    }
}

解題方法2

  • 既然可以使用棧解決,那麼很自然想到可以使用遞歸來解決,對於每一層遞歸只打印第一個節點元素。
public class Test {
    public static void main(String[] args) throws Exception {
        int[] arr = {10,20,30,40,50};
        Node head = create(arr);
        show(head.next);
    }
    //逆序打印
    public static void show(Node head){
        if(head==null){
            return;
        }
        show(head.next); //對於每一層遞歸都是先打印後續部分再打印第一個節點
        System.out.println(head.val);
    }

    //創建帶頭節點單鏈表
    public static Node create(int[] arr){
        Node head = new Node(0); //頭節點
        Node newnode = null; //指向新節點
        Node tail = head; //指向鏈表尾節點
        for(int a:arr){
            newnode = new Node(a);
            newnode.next = tail.next;
            tail.next = newnode;
            tail = newnode;
        }
        return head;
    }
}
class Node{
    int val;
    Node next;
    Node(int val){
        this.val = val;
    }
}

解題方法3

  • 我們也可以將鏈表反轉再順序打印,最後再將鏈表還原。
  • 此方法時間複雜度也爲n,但是係數爲3。
public class Test {
    public static void main(String[] args) throws Exception {
        int[] arr = {10,20,30,40,50};
        Node head = create(arr);
        show(head);
    }
    //逆序打印
    public static void show(Node head){
        reverse(head); //打印前反轉
        for(Node p=head.next;p!=null;p=p.next){
            System.out.print(p.val+" ");
        }
        reverse(head);//打印後還原
    }

    //反轉鏈表
    public static Node reverse(Node head){
        if(head==null || head.next==null){
            return head;
        }
        Node p = head.next;
        head.next = null;
        while(p!=null){
            Node temp = p.next;
            p.next = head.next;
            head.next = p;
            p=temp;
        }
        return head;
    }
    //創建單鏈表
    public static Node create(int[] arr){
        Node head = new Node(0); //頭節點
        Node newnode = null; //指向新節點
        Node tail = head; //指向鏈表尾節點
        for(int a:arr){
            newnode = new Node(a);
            newnode.next = tail.next;
            tail.next = newnode;
            tail = newnode;
        }
        return head;
    }
}
class Node{
    int val;
    Node next;
    Node(int val){
        this.val = val;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章