【6. 从尾到头打印链表无标题】


comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9806.%20%E4%BB%8E%E5%B0%BE%E5%88%B0%E5%A4%B4%E6%89%93%E5%8D%B0%E9%93%BE%E8%A1%A8/README.md

面试题 06. 从尾到头打印链表

题目描述

输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。

 

示例 1:

输入:head = [1,3,2]
输出:[2,3,1]

 

限制:

0 <= 链表长度 <= 10000

解法

方法一:顺序遍历 + 反转

我们可以顺序遍历链表,将每个节点的值存入数组中,然后将数组反转。

时间复杂度 O ( n ) O(n) O(n),空间复杂度 O ( n ) O(n) O(n)。其中 n n n 为链表的长度。

Python3
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None


class Solution:
    def reversePrint(self, head: ListNode) -> List[int]:
        ans = []
        while head:
            ans.append(head.val)
            head = head.next
        return ans[::-1]
Java
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        Deque<Integer> stk = new ArrayDeque<>();
        for (; head != null; head = head.next) {
            stk.push(head.val);
        }
        int[] ans = new int[stk.size()];
        for (int i = 0; !stk.isEmpty(); ++i) {
            ans[i] = stk.pop();
        }
        return ans;
    }
}
C++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        vector<int> ans;
        for (; head; head = head->next) {
            ans.push_back(head->val);
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};
Go
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func reversePrint(head *ListNode) (ans []int) {
	for ; head != nil; head = head.Next {
		ans = append(ans, head.Val)
	}
	for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
		ans[i], ans[j] = ans[j], ans[i]
	}
	return
}
TypeScript
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function reversePrint(head: ListNode | null): number[] {
    const ans: number[] = [];
    for (; head; head = head.next) {
        ans.push(head.val);
    }
    return ans.reverse();
}
Rust
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
//   pub val: i32,
//   pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
//   #[inline]
//   fn new(val: i32) -> Self {
//     ListNode {
//       next: None,
//       val
//     }
//   }
// }
impl Solution {
    pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
        let mut ans: Vec<i32> = vec![];
        let mut cur = head;
        while let Some(node) = cur {
            ans.push(node.val);
            cur = node.next;
        }
        ans.reverse();
        ans
    }
}
JavaScript
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {number[]}
 */
var reversePrint = function (head) {
    const ans = [];
    for (; head; head = head.next) {
        ans.push(head.val);
    }
    return ans.reverse();
};
C#
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public int[] ReversePrint(ListNode head) {
        List<int> ans = new List<int>();
        for (; head != null; head = head.next) {
            ans.Add(head.val);
        }
        ans.Reverse();
        return ans.ToArray();
    }
}
Swift
/* public class ListNode {
*    public var val: Int
*    public var next: ListNode?
*    public init(_ val: Int) {
*        self.val = val
*        self.next = nil
*    }
* }
*/

class Solution {
    func reversePrint(_ head: ListNode?) -> [Int] {
        var stack = [Int]()
        var current = head
        while let node = current {
            stack.append(node.val)
            current = node.next
        }

        return stack.reversed()
    }
}

方法二:递归

我们可以使用递归的方式,先递归得到 head 之后的节点反过来的值列表,然后将 head 的值加到列表的末尾。

时间复杂度 O ( n ) O(n) O(n),空间复杂度 O ( n ) O(n) O(n)。其中 n n n 为链表的长度。

当然!我们可以通过一个例子来说明这个递归函数 reversePrint 是如何工作的。

假设我们有一个链表:1 -> 2 -> 3 -> None。

我们调用 reversePrint 函数并通过递归逆序打印链表的节点值。

详细解释:

  1. 初始调用:reversePrint(head),其中 head 指向链表的第一个节点(值为 1)。
  2. 检查 head 是否为 None。因为 head 不为 None,继续递归调用:reversePrint(head.next),此时 head.next 指向第二个节点(值为 2)。
  3. 再次递归调用:reversePrint(head.next),此时 head.next 指向第三个节点(值为 3)。
  4. 再次递归调用:reversePrint(head.next),此时 head.nextNone

headNone 时,函数返回一个空列表 []

回溯开始:

  1. 第四步的返回值是 [],将当前节点(值为 3)加入列表,结果是 [3]
  2. 第三步的返回值是 [3],将当前节点(值为 2)加入列表,结果是 [3, 2]
  3. 第二步的返回值是 [3, 2],将当前节点(值为 1)加入列表,结果是 [3, 2, 1]

最终返回 [3, 2, 1],这是链表的逆序打印结果。

通过这个例子,可以清楚地看到递归如何将链表节点值逆序添加到结果列表中。

Python3
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None


class Solution:
    def reversePrint(self, head: ListNode) -> List[int]:
        if head is None:
            return []
        ans = self.reversePrint(head.next) #先递进去,直到遇到终止条件,得到空列表
        ans.append(head.val) #倒着回溯,逆序添加元素
        return ans
Java
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        int n = 0;
        ListNode cur = head;
        for (; cur != null; cur = cur.next) {
            ++n;
        }
        int[] ans = new int[n];
        cur = head;
        for (; cur != null; cur = cur.next) {
            ans[--n] = cur.val;
        }
        return ans;
    }
}
C++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        if (!head) {
            return {};
        }
        vector<int> ans = reversePrint(head->next);
        ans.push_back(head->val);
        return ans;
    }
};
Go
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func reversePrint(head *ListNode) (ans []int) {
	if head == nil {
		return
	}
	ans = reversePrint(head.Next)
	ans = append(ans, head.Val)
	return
}
Rust
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
//   pub val: i32,
//   pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
//   #[inline]
//   fn new(val: i32) -> Self {
//     ListNode {
//       next: None,
//       val
//     }
//   }
// }
impl Solution {
    pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
        let mut cur = &head;
        let mut n = 0;
        while let Some(node) = cur {
            cur = &node.next;
            n += 1;
        }

        let mut arr = vec![0; n];
        let mut cur = head;
        while let Some(node) = cur {
            n -= 1;
            arr[n] = node.val;
            cur = node.next;
        }
        arr
    }
}
JavaScript
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {number[]}
 */
var reversePrint = function (head) {
    if (!head) {
        return [];
    }
    const ans = reversePrint(head.next);
    ans.push(head.val);
    return ans;
};
  • 17
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值