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]

总结:

LeetCode-剑指Offer-06-从尾到头打印链表_第1张图片


你可能感兴趣的:(#,剑指Offer)