zoukankan      html  css  js  c++  java
  • 面试题5:从尾到头打印链表

     1 /*1.递归解决*/
     2 bool reversePrintA(List_Node * listNode)
     3 {
     4     if(NULL == listNode)
     5     {
     6         return false;
     7     }
     8 
     9     if(NULL != listNode->next)
    10     {
    11         reversePrintA(listNode->next);
    12     }
    13     printf("%d
    ", listNode->value);
    14     return true;
    15 }
    16 /*2.使用栈*/
    17 bool reversePrintB(List_Node * listNode)
    18 {    
    19     int listNum = 0;
    20     int listValue[30];
    21 
    22     if(NULL == listNode)
    23     {
    24         return false;
    25     }
    26 
    27     while(listNode->next != NULL)
    28     {
    29         listValue[listNum++] = listNode->value;
    30         listNode = listNode->next;
    31     }
    32 
    33     listValue[listNum] = listNode->value;
    34 
    35     for(;listNum >= 0; listNum--)
    36     {
    37         printf("%d
    ", listValue[listNum]);
    38     }
    39 
    40     return true;
    41 }

      使用递归的好处是:代码简洁;但是链表过大,嵌套过深,会导致栈溢出;使用栈的好处是:不需要担心栈溢出,典型的空间换时间,需要额外的存储空间。

  • 相关阅读:
    python023
    python019
    python018
    python017
    python016
    python013
    python012
    数据库,盘它!!!!
    网络编程
    面向对象简述
  • 原文地址:https://www.cnblogs.com/cauchy007/p/4558989.html
Copyright © 2011-2022 走看看