P51、面试题5:从尾到头打印链表
题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值。 链表结点定义如下: Struct ListNode{ int m_nKey; ListNode* m_pNext; }; |
我们可以用栈来实现“后进先出”的顺序。每经过一个结点的时候,把该结点防到一个栈中。当遍历完整个链表后,再从栈顶开始逐个输出结点的值,此时输出的结点的顺序已经反转过来了。
void PrintListReversingly_Iteratively(ListNode* pHead){
std::stack<ListNode*> node;
ListNode* pNode = pHead;
while(pNode != null){
nodes.push(pNode);
pNode = pNode->m_pNext;
}
while(!nodes.empty()){
pNode = nodes.top();
printf("%d\t",pNode->m_nValue);
nodes.pop();
}
}
我们也可以用递归来实现反过来输出链表,我们每访问到一个结点的时候,先递归输出它后面的结点,再输出该结点自身,这样链表的输出结果就反过来了。
void PrintListReversingly_Recursively(ListNode* pHead){
if(pHead != null){
if(pHead->m_pNext != null){
PrintListReversingly_Recursively(pHead->m_pNext);
}
printf("%d\t",pHead->m_nValue);
}
}
java精简版:
Node类:
package com.yyq; /**
* Created by Administrator on 2015/9/8.
*/
public class Node {
String value;
Node next;
public Node(String value){
this.value = value;
} public String getValue() {
return value;
} public Node getNext() {
return next;
} public void setNext(Node next) {
this.next = next;
} public void setValue(String value) {
this.value = value;
}
};
处理类:
package com.yyq;
import java.util.Stack; /**
* Created by Administrator on 2015/9/8.
*/
public class PrintLinkReversingly {
public static void main(String[] args) {
Node a = new Node("A");
Node b = new Node("B");
Node c = new Node("C");
Node d = new Node("D");
Node e = new Node("E");
Node f = new Node("F");
Node g = new Node("G");
a.next = b;
b.next = c;
c.next = d;
d.next = e;
e.next = f;
f.next = g;
printTailToStartRec(a);
printTailToStartStack(a);
} public static void printTailToStartRec(Node start) {
if (start == null ) return;
if (start.next!= null) {
printTailToStartRec(start.next);
}
System.out.println(start.value);
} private static void printTailToStartStack(Node node) {
if (node == null) {
System.out.println("list is null");
return;
} Stack<Node> stack = new Stack<Node>();
while (node != null) {
stack.push(node);
node = node.next;
}
while (!stack.isEmpty()) {
System.out.println(stack.pop().value);
}
}
}
输出结果:
F E D C B A G F E D C B A Process finished with exit code 0 |
java版本:
链表接口定义:
package com.yyq; /**
* Created by Administrator on 2015/9/4.
*/
public interface Link {
//向链表增加数据
void add(Object data); //可以增加一组对象
void add(Object data[]); //在链表中删除数据
void delete(Object data); //判断数据是否存在
boolean exists(Object data); //取得全部的保存对象
Object[] getAll(); //根据保存的位置取出指定对象
Object get(int index); //求出链表的长度
int length();
}
链表类定义:
package com.yyq; /**
* Created by Administrator on 2015/9/4.
*/
public class LinkImpl implements Link {
class Node {
private Object data;
private Node next; public Node(Object data) {
this.data = data;
} public void addNode(Node newNode) {
if (this.next == null) {
this.next = newNode;
} else {
this.next.addNode(newNode);
}
} public void deleteNode(Node previous, Object data) {
if (this.data.equals(data)) {
previous.next = this.next;
} else {
if (this.next != null) {
this.next.deleteNode(this, data);
}
}
} public void getAll() {
retdata[foot++] = this.data; //取出当前节点中的数据
if (this.next != null) {
this.next.getAll();
}
}
};
private int foot = 0;
private Node root; //根节点
private int len;
private Object retdata[];//接收全部的返回值数据 //向链表增加数据
@Override
public void add(Object data) {
if (data != null) {
len++; //保存个数
Node newNode = new Node(data);
if (this.root == null) {
this.root = newNode;
} else {
this.root.addNode(newNode);
}
}
} //可以增加一组对象
@Override
public void add(Object data[]) {
for(int x = 0; x < data.length; x++){
this.add(data[x]);
}
} //在链表中删除数据
@Override
public void delete(Object data) {
if(this.exists(data)){//如果存在,则执行删除
if(this.root.equals(data)){
this.root = this.root.next;
}else {
this.root.next.deleteNode(this.root,data);
}
}
} //判断数据是否存在
@Override
public boolean exists(Object data) {
if(data == null){
return false;
}
if(this.root == null){
return false;
}
Object d[] = this.getAll();//取得全部的数据
boolean flag = false;
for(int x = 0; x < d.length; x++){
if(data.equals(d[x])){
flag = true;
break;
}
}
return flag;
} //取得全部的保存对象
@Override
public Object[] getAll() {
this.foot = 0;
if(this.len != 0){
this.retdata = new Object[this.len];//根据大小开辟数组
this.root.getAll();
return this.retdata;
}else{
return null;
}
} //根据保存的位置取出指定对象
@Override
public Object get(int index) {
Object d[] = this.getAll();
if(index < d.length){
return d[index];
}else{
return null;
}
} //求出链表的长度
@Override
public int length() {
return this.len;
}
}
链表使用举例:
package com.yyq; /**
* Created by Administrator on 2015/9/4.
*/
public class PrintListReversingly {
public static void main(String[] args) { Link link = new LinkImpl();
link.add("A");
link.add("B");
link.add("C");
link.add(new String[]{"X","Y"});
Object obj[] = link.getAll();
for(Object o : obj){
System.out.println(o);
}
System.out.println(obj.length);
System.out.println(link.exists(null));
System.out.println(link.get(3));
link.delete("X");
obj = link.getAll();
for(Object o : obj){
System.out.println(o);
}
System.out.println(obj.length); //注意这里还是原来obj开辟时的长度
}
}
从尾到头打印链表:
package com.yyq; import java.util.Stack; /**
* Created by Administrator on 2015/9/4.
*/
public class PrintListReversingly {
public static void main(String[] args) {
Link link = new LinkImpl();
link.add("A");
link.add("B");
link.add("C");
link.add(new String[]{"D","E","F","G"});
Object obj[] = link.getAll();
for(Object o : obj){
System.out.println(o);
} int len = obj.length;
System.out.println("============");
for(int i = len-1; i >= 0; i--){
System.out.println(obj[i]);
} }
}
P51、面试题5:从尾到头打印链表的更多相关文章
- 【剑指offer】面试题 6. 从尾到头打印链表
面试题 6. 从尾到头打印链表 NowCoder 题目描述 输入一个链表的头结点,从尾到头反过来打印出每个结点的值. Java 实现 ListNode Class class ListNode { i ...
- 剑指offer-面试题5.从尾到头打印链表
题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值. 刚看到这道题的小伙伴可能就会想,这还不简单,将链表反转输出. 但是这种情况破坏了链表的结构. 如果面试官要求不破坏链表结构呢,这时候我们 ...
- 前端常见算法面试题之 - 从尾到头打印链表[JavaScript解法]
题目描述 输入一个链表的头结点,从尾到头反过来打印出每个结点的值 实现思路 前端工程师看到这个题目,直接想到的就是,写个while循环来遍历链表,在循环中把节点的值存储在数组中,最后在把数组倒序后,遍 ...
- 剑指offer_面试题5_从尾到头打印链表(栈和递归实现)
题目:输入一个链表的头结点,从尾到头反过来打印出每一个节点的值 考察 单链表操作.栈.递归等概念. 理解:要实现单链表的输出,那么就须要遍历.遍历的顺序是从头到尾.而节点输出的顺序是从尾到头.因此,先 ...
- 《剑指offer》面试题5—从尾到头打印链表
重要思路: 这个问题肯定要遍历链表,遍历链表的顺序是从头到尾,而要输出的顺序却是从尾到头,典型的“后进先出”,可以用栈实现. 注意stl栈的使用,遍历stack的方法. #include <io ...
- 【剑指Offer】面试题06.从尾到头打印链表
题目 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回). 示例 1: 输入:head = [1,3,2] 输出:[2,3,1] 限制: 0 <= 链表长度 <= 1000 ...
- 《剑指offer》面试题06. 从尾到头打印链表
问题描述 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回). 示例 1: 输入:head = [1,3,2] 输出:[2,3,1] 限制: 0 <= 链表长度 <= 10 ...
- 剑指Offer:面试题5——从尾到头打印链表(java实现)
问题描述:输入一个链表的头结点,从尾巴到头反过来打印出每个结点的值. 首先定义链表结点 public class ListNode { int val; ListNode next = null; L ...
- 《剑指offer》面试题5 从尾到头打印链表 Java版
书中方法一:反转应该立刻想到栈,利用一个栈完成链表的反转打印,但是用了额外的O(n)空间. public void printFromTail(ListNode first){ Stack<Li ...
随机推荐
- ASP.NET的错误处理机制
对于一个Web应用程序来说,出错是在所难免的,因此我们应该未雨绸缪,为可能出现的错误提供恰当的处理.事实上,良好的错误处理机制正是衡量Web应用程序好坏的一个重要标准.试想一下,当用户不小心在浏览器输 ...
- jQuery Easyui DataGrid应用
冻结列 $('#tbList').datagrid({ pagination: true, frozenColumns: [[ { field: 'BId',checkbox:'true',width ...
- Sublime Text 3 使用备注
去年开始为了正规化自己的日常编辑工作,在dw,editplus,notap++,st里做了个选择,最终决定改曾经的dw为st. 毕竟dw是上个世纪的东西了,体积比较臃肿了.所以,在这里记录关于st的使 ...
- AjaxFileUpload Firefox 不工作异常 (zero-width space characters from a JavaScript string)
Firefox 返回的提示报错parse error (Chrome 和 IE正常) 打印出来返回的字符串,目测正常 将字符串放入notepad++, 转换字符集为ANSI 发现多出了欧元符号 通过j ...
- Android 生命周期 和 onWindowFocusChanged
转载 http://blog.csdn.net/pi9nc/article/details/9237031 onWindowFocusChanged重要作用 Activity生命周期中,onStart ...
- 使用FileResult导出txtl数据文件
public FileResult ExportMobileNoTxt(SearchClientModel model){ var sbTxt = new StringBuilder(); ; i & ...
- select 函数实现 三种拓扑结构 n个客户端的异步通信 (完全图+线性链表+无环图)
一.这里只介绍简单的三个客户端异步通信(完全图拓扑结构) //建立管道 mkfifo open顺序: cl1 读 , cl2 cl3 向 cl1写 cl2 读 , cl1 cl3 向 cl2写 cl3 ...
- 浅谈string
#include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 using std::string;using ...
- easy ui easyui-linkbutton 禁用、启用
<a id="btn_update_Shop" name="btn_update_Shop" class="easyui-linkbutton& ...
- EXTJS store 某行某列数据更新等操作
1.可以使用add(Ext.data.Record[] records)或者add(Ext.data.Record record)向store末尾添加一个或多个record.如: var newRec ...