面试题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. 从尾到头打印链表(递归法、辅助栈法,清晰图解)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章