LeetCode刷题--点滴记录006

Posted 鲁棒最小二乘支持向量机

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode刷题--点滴记录006相关的知识,希望对你有一定的参考价值。

6. 剑指 Offer 06. 从尾到头打印链表

要求

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

解题

C++版本

#include <iostream>
using namespace std;
#include <vector>

struct ListNode 
    int val;        // 节点值
    ListNode* next; // 后继节点引用
    ListNode() : val(0), next(nullptr) 
    ListNode(int x) : val(x), next(NULL) 
;

class Solution 
public:
    vector<int> printListFromTailToHead01(ListNode* head) 
        vector<int> array;
        if (head == NULL)
        
            return array;
        
        while (head != NULL)
        
            array.push_back(head->val);
            head = head->next;
        
        //翻转
        reverse(array.begin(), array.end());
        return array;
    
    vector<int> printListFromTailToHead02(ListNode* head) 
        vector<int> array;
        ListNode* cur = head;
        ListNode* prev = head;
        if (head == NULL)
        
            return array;
        
        while (head->next != NULL)
        
            cur = head->next;
            head->next = cur->next;
            cur->next = prev;
            prev = cur;
        
        while (cur)
        
            array.push_back(cur->val);
            cur = cur->next;
        
        return array;
    
;

void print(ListNode* phead)//输出

    ListNode* p = phead;
    while (p != NULL)
    
        cout << p->val << " ";
        p = p->next;
    
    cout << endl;


void printVector(vector<int>& v)

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	
		cout << *it << " ";
	
	cout << endl;

void test01()

    Solution s;
    //实例化节点
    ListNode* n1 = new ListNode(1); // 节点 head
    ListNode* n2 = new ListNode(2);
    ListNode* n3 = new ListNode(3);
    ListNode* n4 = new ListNode(4);
    ListNode* n5 = new ListNode(5);

    // 构建引用指向
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = n5;
    print(n1);
    vector<int> array1 = s.printListFromTailToHead01(n1);
    printVector(array1);
    vector<int> array2 = s.printListFromTailToHead02(n1);
    printVector(array2);


int main()

    test01();

    system("pause");
    return 0;

Python版本

class ListNode:
    def __init__(self, x):
        self.val = x     # 节点值
        self.next = None # 后继节点引用
        
def print_ListNode(node):
    while node:
        print(node.val, end=' ')
        node = node.next
    print()    
class Solution:
    def reversePrint01(self, head: ListNode):
        # 递归的终止条件
        if not head :
            return []
        else:
            return self.reversePrint01(head.next) + [head.val]
        
    def reversePrint02(self, head: ListNode):
       res = []
       if not head:
           return res  
       while head:
           res.append(head.val)
           head = head.next  
       res.reverse()   
       return res 
    
    def reversePrint03(self, head: ListNode):
        stack = []
        while head:
            stack.append(head.val)
            head = head.next
        return stack[::-1]
       
def test01():
    solution = Solution()
    # 实例化节点
    n1 = ListNode(1) # 节点 head
    n2 = ListNode(2)
    n3 = ListNode(3)
    n4 = ListNode(4)
    n5 = ListNode(5)

    # 构建引用指向
    n1.next = n2
    n2.next = n3
    n3.next = n4
    n4.next = n5
    print_ListNode(n1)

    print(solution.reversePrint01(n1))
    
    print(solution.reversePrint02(n1))
    
    print(solution.reversePrint03(n1))
            
if __name__=="__main__":
    test01()

Java版本

package com.hailei_01;

import java.util.LinkedList;

public class reversePrint 
    public static void main(String[] args) 
        // 实例化节点
        ListNode n1 = new ListNode(1); // 节点 head
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);

        // 构建引用指向
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        printListNode(n1);
        for (int i=0; i<reversePrint01(n1).length; i++ )
            System.out.print(reversePrint01(n1)[i]+" ");
        System.out.println();
        for (int i=0; i<reversePrint02(n1).length; i++ )
            System.out.print(reversePrint02(n1)[i]+" ");
    

    public static class ListNode 
        int val;       // 节点值
        ListNode next; // 后继节点引用
        ListNode(int x)  val = x; 
    

    public static void printListNode(ListNode headNode) 
        ListNode listNode = headNode;
        while(listNode!=null) 
            System.out.print(listNode.val+" ");
            listNode = listNode.next;
        
        System.out.println();
    

    public static int[] reversePrint01(ListNode head)
        ListNode tmp = head;
        int len = 0;
        while(tmp!=null)
            len++;
            tmp = tmp.next;
        
        int[] res = new int[len];
        tmp = head;
        int index = len-1;
        while(tmp!=null)
            res[index--] = tmp.val;
            tmp = tmp.next;
        
        return res;
    

    public static int[] reversePrint02(ListNode head) 
        LinkedList<Integer> stack = new LinkedList<Integer>();
        while(head != null) 
            stack.addLast(head.val);
            head = head.next;
        
        int[] res = new int[stack.size()];
        for(int i = 0; i < res.length; i++)
            res[i] = stack.removeLast();
        return res;
    



希望本文对大家有帮助,上文若有不妥之处,欢迎指正

分享决定高度,学习拉开差距

以上是关于LeetCode刷题--点滴记录006的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode刷题--点滴记录017

LeetCode刷题--点滴记录015

LeetCode刷题--点滴记录005

LeetCode刷题--点滴记录003

LeetCode刷题--点滴记录009

LeetCode刷题--点滴记录001