输入一个链表,从尾到头打印链表每个节点的值。
2017-06-22 16:17
344 查看
这是剑指offer上的题。首先要弄明白链表的头和尾。单向链表只能从头开始查找。最简单的做法,就是将链表中的元素从头读到尾,存到vector中。然后利用reverse函数反转vector。代码如下。
另外一种做法利用stack。先用stack存储链表的各个节点的指针,再将stack中的指针依次出站,找到指针对应的数值,存入vector,就实现了list的翻转。
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> list;
stack<struct ListNode*> nodes;
//cout << "下面是原链表数据,从头到尾" << endl;
while(head != NULL)
{
cout << head->val << " ";
nodes.push(head);
head = head->next;
}
cout << endl;
struct ListNode* pTop = head;
while(!nodes.empty()){
pTop = nodes.top();
list.push_back(pTop->val);
nodes.pop();
}
//reverse(list.begin(),list.end()); //reverse()函数用于反转vector数组
return list;
}
};
为了进行测试,下面提供完整测试代码,包含main函数.可直接拷贝使用。
// staticTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> list;
cout << "下面是原链表数据,从头到尾" << endl;
while(head != NULL)
{
cout << head->val << " ";
list.push_back(head->val);
head = head->next;
}
cout << endl;
reverse(list.begin(),list.end()); //reverse()函数用于反转vector数组
return list;
}
};
void printintVector(vector<int> &vt ){
vector<int>::iterator it = vt.begin();
cout << "下面是链表数据的翻转,翻转为从尾到头" << endl;
for(;it != vt.end();it++ ){
cout << *it << " ";
}
cout << endl;
}
struct ListNode* creatList(int n){
struct ListNode* head;
head=NULL;
for(int i = 0; i < n; i++){
struct ListNode *t = (struct ListNode*)malloc(sizeof(struct ListNode));
t->val = i;
if(head == NULL){
head = t;
head->next = NULL;
}
else{
t->next = head;
head = t;
}
}
return head;
}
int _tmain(int argc, _TCHAR* argv[])
{
struct ListNode *head;
head = creatList(19);
Solution s;
vector<int> result;
result = s.printListFromTailToHead(head);
printintVector(result);
system("pause");
return 0;
}
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector<int> list; while(head != NULL) { list.push_back(head->val); head = head->next; } reverse(list.begin(),list.end()); //reverse()函数用于反转vector数组 return list; } };
另外一种做法利用stack。先用stack存储链表的各个节点的指针,再将stack中的指针依次出站,找到指针对应的数值,存入vector,就实现了list的翻转。
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> list;
stack<struct ListNode*> nodes;
//cout << "下面是原链表数据,从头到尾" << endl;
while(head != NULL)
{
cout << head->val << " ";
nodes.push(head);
head = head->next;
}
cout << endl;
struct ListNode* pTop = head;
while(!nodes.empty()){
pTop = nodes.top();
list.push_back(pTop->val);
nodes.pop();
}
//reverse(list.begin(),list.end()); //reverse()函数用于反转vector数组
return list;
}
};
为了进行测试,下面提供完整测试代码,包含main函数.可直接拷贝使用。
// staticTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> list;
cout << "下面是原链表数据,从头到尾" << endl;
while(head != NULL)
{
cout << head->val << " ";
list.push_back(head->val);
head = head->next;
}
cout << endl;
reverse(list.begin(),list.end()); //reverse()函数用于反转vector数组
return list;
}
};
void printintVector(vector<int> &vt ){
vector<int>::iterator it = vt.begin();
cout << "下面是链表数据的翻转,翻转为从尾到头" << endl;
for(;it != vt.end();it++ ){
cout << *it << " ";
}
cout << endl;
}
struct ListNode* creatList(int n){
struct ListNode* head;
head=NULL;
for(int i = 0; i < n; i++){
struct ListNode *t = (struct ListNode*)malloc(sizeof(struct ListNode));
t->val = i;
if(head == NULL){
head = t;
head->next = NULL;
}
else{
t->next = head;
head = t;
}
}
return head;
}
int _tmain(int argc, _TCHAR* argv[])
{
struct ListNode *head;
head = creatList(19);
Solution s;
vector<int> result;
result = s.printListFromTailToHead(head);
printintVector(result);
system("pause");
return 0;
}
相关文章推荐
- 输入一个链表,从尾到头打印链表每个节点的值。
- 输入一个链表,从尾到头打印链表每个节点的值
- 输入一个链表的头结点,从尾到头反过来打印出每个节点的值
- 输入一个链表,从尾到头打印链表每个节点的值
- 输入一个链表,从尾到头打印链表每个节点的值。
- 输入一个链表,从尾到头打印链表每个节点的值
- <剑指Offer>输入一个链表,从尾到头打印链表每个节点的值---Java版本
- 两种方法实现:输入一个链表,从尾到头打印链表每个节点的值
- 输入一个链表,从尾到头打印链表每个节点的值。
- 2.输入一个链表,从尾到头打印链表每个节点的值
- 输入一个链表,从尾到头打印链表每个节点的值。
- 输入一个链表的头结点,从尾到头反过来打印每个节点的值。
- 输入一个链表,从尾到头打印链表每个节点的值
- 输入一个链表,从尾到头打印链表每个节点的值。
- 题目:输入一个链表,从尾到头打印链表每个节点的值
- 输入一个链表,从尾到头打印链表每个节点的值。
- 输入一个链表,从尾到头打印链表每个节点的值
- 输入一个链表,从尾到头打印链表每个节点的值。
- 输入一个链表的头结点,从尾到头反过来打印出每个节点的值。
- 输入一个链表的头结点,从尾到头反过来打印每个节点的值。