剑指offer---从尾到头打印链表

问题:剑指offer---从尾到头打印链表

要求:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

 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 };

上一篇博客中已经建立了链表类,基于上述基础进行适当修改即可解决上述问题。

方法一:采用链表类反转链表方法的思想,采用三指针法将链表指针倒序(但是此方法改变了链表的结构);

方法二:先将链表从头到尾遍历,存到堆栈中,利用堆栈后进先出的特点,依次弹出就可。(没有对原链表进行改动,本文采用方法二)。

大部分代码与上篇博客中的链表类相同,只是增加了一个 printListFromTailToHead(ListNode* head) 函数,同时把 class List() 的 Node *head; 改为了 public 

其中 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 }

完整代码

  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

运行结果

 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]
原文地址:https://www.cnblogs.com/iwangzhengchao/p/9773960.html