面試題06. 從尾到頭打印鏈表

題目:

面試題06. 從尾到頭打印鏈表
在這裏插入圖片描述

題解:

1. 題解一:

用一個指針遍歷鏈表長度得出數組容量,然後從數組的最後一位開始填充。

2. 題解二:

反轉鏈表,同時記錄鏈表長度,翻轉完成後創建數組,用翻轉後的鏈表給數組賦值,返回數組。

代碼:

1. 代碼一:

/**
 * 面試題06
 */
public class 面試題06 {

    // 解法1:
    public static int[] reversePrint(ListNode head) {
        ListNode cur = head;
        int len = 0;
        while (cur != null) {
            len++;
            cur = cur.next;
        }
        int res[] = new int[len];
        ListNode node = head;
        for (int i = len - 1; i >= 0; i--) {
            res[i] = node.val;
            node = node.next;
        }
        return res;
    }

    private static ListNode createLinkedList(int[] arr) {// 將輸入的數組輸入到鏈表中
        if (arr.length == 0) {
            return null;
        }
        ListNode head = new ListNode(arr[0]);
        ListNode current = head;
        for (int i = 1; i < arr.length; i++) {// 過程
            current.next = new ListNode(arr[i]);
            current = current.next;
        }
        return head;
    }

    private static void printLinkedList(ListNode head) {// 將鏈表結果打印
        ListNode current = head;
        while (current != null) {
            System.out.printf("%d -> ", current.val);
            current = current.next;
        }
        System.out.println("NULL");
    }

    public static void main(String[] args) {
        int x[] = { 1, 3, 2 };
        ListNode list = createLinkedList(x);
        printLinkedList(list);
        int res[] = reversePrint(list);
        for (int i = 0; i < res.length; i++) {
            System.out.print(res[i] + " ");
        }
        System.out.println();
    }
}

2. 代碼二:

/**
 * 面試題06
 */
public class 面試題06 {

    // 解法2:
    public static int[] reversePrint(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        ListNode temp = null;
        int len = 0;
        while (cur != null) {
            temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
            len++;
        }
        int res[] = new int[len];
        ListNode node = pre;
        for (int i = 0; i < len; i++) {
            res[i] = node.val;
            node = node.next;
        }
        return res;
    }

    private static ListNode createLinkedList(int[] arr) {// 將輸入的數組輸入到鏈表中
        if (arr.length == 0) {
            return null;
        }
        ListNode head = new ListNode(arr[0]);
        ListNode current = head;
        for (int i = 1; i < arr.length; i++) {// 過程
            current.next = new ListNode(arr[i]);
            current = current.next;
        }
        return head;
    }

    private static void printLinkedList(ListNode head) {// 將鏈表結果打印
        ListNode current = head;
        while (current != null) {
            System.out.printf("%d -> ", current.val);
            current = current.next;
        }
        System.out.println("NULL");
    }

    public static void main(String[] args) {
        int x[] = { 1, 3, 2 };
        ListNode list = createLinkedList(x);
        printLinkedList(list);
        int res[] = reversePrint(list);
        for (int i = 0; i < res.length; i++) {
            System.out.print(res[i] + " ");
        }
        System.out.println();
    }
}

參考:

  1. 面試題06. 從尾到頭打印鏈表
  2. 面試題06. 從尾到頭打印鏈表(遞歸法、輔助棧法,清晰圖解)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章