劍指offer---從尾到頭打印鏈表

問題:劍指offer---從尾到頭打印鏈表html

要求:輸入一個鏈表,按鏈表值從尾到頭的順序返回一個ArrayList。node

 1 /**  2 * struct ListNode {  3 * int val;  4 * struct ListNode *next;  5 * ListNode(int x) :  6 * val(x), next(NULL) {  7 * }  8 * };  9 */
10 class Solution { 11 public: 12     vector<int> printListFromTailToHead(ListNode* head) { 13         
14  } 15 };

上一篇博客中已經創建了鏈表類,基於上述基礎進行適當修改便可解決上述問題。ios

方法一:採用鏈表類反轉鏈表方法的思想,採用三指針法將鏈表指針倒序(可是此方法改變了鏈表的結構);ide

方法二:先將鏈表從頭至尾遍歷,存到堆棧中,利用堆棧後進先出的特色,依次彈出就可。(沒有對原鏈表進行改動,本文采用方法二)。函數

大部分代碼與上篇博客中的鏈表類相同,只是增長了一個 printListFromTailToHead(ListNode* head) 函數,同時把 class List() 的 Node *head; 改成了 public spa

其中 printListFromTailToHead(ListNode* head) 函數以下:指針

 1 vector<int> printListFromTailToHead(Node* head){  2     stack<int> temp;  3     Node *p = head;  4     while (p != nullptr){  5         temp.push((*p).data);  6         p = p->next;  7  }  8     vector<int> res;  9     while (!temp.empty()){ 10  res.push_back(temp.top()); 11  temp.pop(); 12  } 13     return res; 14 }

完整代碼code

 1 #include<iostream>
 2 #include<vector>
 3 #include<stack>
 4 using namespace std;  5 
 6 class Node {  7 public:  8     int data;  9     Node *next;  10     Node(int da):  11  data(da), next(NULL){}  12 };  13 
 14 class List{  15 public:  16     Node *head;  17  List(): head(NULL){}  18     ~List(){  19         delete head;  20         cout<<"The list is deleted."<<endl;  21  };  22     int size();  23     void printList(); // 打印鏈表
 24     void insert(int position, int value); // 指定位置插入
 25     void insertHead(int value); // 插入到最前
 26     void insertTail(int value); // 插入到最後
 27 };  28 
 29 // 返回鏈表大小
 30 int List::size(){  31     Node *p = head;  32     int index = 0;  33     while(p != NULL){  34         index++;  35         p = p->next;  36  }  37     return index;  38 }  39 
 40 // 打印鏈表
 41 void List::printList(){  42     Node *p = head;  43     while(p != NULL){  44         cout<<p->data<<" ";  45         p = p->next;  46  }  47     cout<<endl;  48     cout<<endl;  49 }  50 
 51 // 在position位置插入value
 52 void List::insert(int position, int value){  53     if(position<0 || position>List::size()){  54         cout<<"position error, please check."<<endl;  55         return ;  56  }  57     Node *s = new Node(value); // new node
 58     Node *p = head;  59     if(head == NULL){ // isEmpty = true
 60         head = s;  61  }  62     else{ // isEmpty = false
 63         if(position == 0){  64             s->next = p;  65             head = s;  66  }  67         else{  68             int index = 0;  69             while(index != position-1){  70                 p = p->next;  71                 index++;  72  }  73             s->next = p->next;  74             p->next = s;  75  }  76  }  77     if (position == 0)  78         cout<<"insert "<<value<<" at the first."<<endl;  79     else if (position == List::size())  80         cout<<"insert "<<value<<" at the tail."<<endl;  81     else
 82         cout<<"insert "<<value<<" at position "<<position<<endl;  83 }  84 
 85 // 頭部插入
 86 void List::insertHead(int value){  87     List::insert(0, value);  88 }  89 
 90 // 尾部插入
 91 void List::insertTail(int value){  92  List::insert(List::size(), value);  93 }  94 
 95 // printListFromTailToHead
 96 vector<int> printListFromTailToHead(Node* head){  97     stack<int> temp;  98     Node *p = head;  99     while (p != nullptr){ 100         temp.push((*p).data); 101         p = p->next; 102  } 103     vector<int> res; 104     while (!temp.empty()){ 105  res.push_back(temp.top()); 106  temp.pop(); 107  } 108     return res; 109 } 110 
111 int main() { 112  List l1; 113     l1.insertTail(6); 114     l1.insertHead(7); 115     l1.insert(1, 5); 116     l1.insert(0, 16); 117     l1.insert(2, 56); 118     l1.insert(0, 169); 119     l1.insert(6, 16); 120 
121     vector<int> result; 122     Node *p = l1.head; 123     result = printListFromTailToHead(p); 124     cout<<endl<<"The list is:"<<endl; 125  l1.printList(); 126     cout<<"print list from tail to head:"<<endl; 127     for(auto &it : result) 128         cout<<it<<' '; 129     cout<<endl<<endl; 130     return 0; 131 }
View Code

運行結果htm

 1 insert 6 at the first.  2 insert 7 at the first.  3 insert 5 at position 1
 4 insert 16 at the first.  5 insert 56 at position 2
 6 insert 169 at the first.  7 insert 16 at position 6
 8 
 9 The list is: 10 169 16 7 56 5 6 16 
11 
12 print list from tail to head: 13 16 6 5 56 7 16 169 
14 
15 The list is deleted. 16 [Finished in 2.6s]
相關文章
相關標籤/搜索