LeetCode-剑指Offer-06-从尾到头打印链表


题意描述:

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

限制: 0 <= 链表长度 <= 10000


示例:

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

解题思路:
Alice: 单向链表怎么反转啊 ?
Bob: 单向链表可以反转啊,我们还做过题目哩,不过反转链表不如反转数组好写。
Alice: 反转数组 ? 可以用Python列表直接切片,或者我们自己交换也可以。
Bob:是的,可以遍历两遍链表,第一遍找到链表中一共有多少个元素,然后一个指针指向数组末尾,往前走,一个链表指向链表头部,往后走,这样一遍下来就可以了。
Alice: 不错不错。😎😎


代码:

Python 方法一: 数组切片

# 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]:
        ret = []
        while head != None:
            ret.append(head.val)
            head = head.next
        return ret[::-1]

Python 方法二: 遍历两次链表 + 数组逆向索引。

# 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]:
        cnt  = 0
        node = head
        while node != None:
            cnt += 1
            node = node.next
        ret = [0 for x in range(cnt)]
        node = head
        while node != None:
            cnt -= 1
            ret[cnt] = node.val
            node = node.next
        return ret
            
        

Python 方法三: 遍历链表 + 逆序数组

# 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]:
        ret = []
        while head != None:
            ret.append(head.val)
            head = head.next
        index = 0
        while index < len(ret) // 2:
            ret[index], ret[len(ret)-1-index] = ret[len(ret)-1-index], ret[index]
            index += 1
        return ret

Java 方法一: ArrayList + 逆向索引。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        
        ArrayList<Integer> tmp = new ArrayList<Integer>();
        ListNode node = head;
        while(node != null){
            tmp.add(node.val);
            node = node.next;
        }
        int[] ret = new int[tmp.size()];
        for(int i=tmp.size()-1; i>=0; --i){
            ret[tmp.size()-1-i] = tmp.get(i);
        }
        return ret;
    }
}

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 cnt = 0;
        ListNode node = head;
        while(node != null){
            cnt++;
            node = node.next;
        }
        int[] ret = new int[cnt];
        node = head;
        while(node != null){
            ret[--cnt] = node.val;
            node = node.next;
        }
        return ret;
    }
}

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 cnt = 0;
        ListNode node = head;
        while(node != null){
            cnt += 1;
            node = node.next;
        }
        int[] ret = new int[cnt];
        node = head;
        int index  = 0;
        while(node != null){
            ret[index++] = node.val;
            node = node.next;
        }
        index = 0;
        int tmp = 0;
        while(index < cnt / 2){
            tmp = ret[index];
            ret[index] = ret[cnt-1-index];
            ret[cnt-1-index] = tmp;
            index += 1;
        }
        return ret;
    }
}

易错点:

  • 一些测试样例:
[]
[1]
[1,2]
[1,3,2]
  • 答案:
[]
[1]
[2,1]
[2,3,1]

总结:

在这里插入图片描述


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值