面试题5:从头到尾打印链表
来源:互联网 发布:美国种族歧视华人 知乎 编辑:程序博客网 时间:2024/05/21 08:41
剑指Offer面试题5:从头到尾打印链表(JS实现)
题目描述:输入一个链表的头结点,从头到尾反过来打印出每个结点的值。
//栈 function Stack() { var arr = []; this.push = function(element) { arr.push(element); }; this.pop = function(){ return arr.pop(); }; this.isEmpty = function() { return arr.length === 0; } } function LinkList() { var Node = function(element) { this.element = element; this.next = null; } length = 0; var head = null; //在结尾插入元素 this.append = function(element) { var node = new Node(element), current; if(head === null) { head = node; }else { current = head; while(current.next) { current = current.next; } current.next = node; } length++; }; //从尾到头打印节点 this.PrintListReversingly = function(){ var stack = new Stack(), current = head, str = ''; while(current) { stack.push(current.element); current = current.next; } while(!stack.isEmpty()){ str += stack.pop(); } return str; } }; var list = new LinkList(); list.append(15); list.append(10); list.append(8); list.append(6); list.append(3); console.log(list.PrintListReversingly());
0 0
- 面试题5 从头到尾打印链表
- 面试题5 从头到尾打印链表
- 面试题5:从头到尾打印链表
- 面试题5:从头到尾打印链表
- 《剑指offer》面试题5从头到尾打印链表
- 《剑指Offer》面试题-从头到尾打印链表
- 剑指offer面试题之从头到尾打印链表
- 剑指offer--面试题6:从头到尾打印链表
- 【剑指offer】面试题6:从头到尾打印链表
- 剑指offer面试题5 从头到尾打印链表(c)
- 剑指offer面试题5 从头到尾打印链表(java)
- 面试题五 从头到尾打印单链表
- 从头到尾打印链表
- 从头到尾打印链表
- 从头到尾打印链表
- 从头到尾打印链表
- 从头到尾打印链表
- 从头到尾打印链表
- Jenkins+svn项目自动打包和部署
- 虚拟手机号接码平台--手机短信验证码平台
- MindManager思维导图实战教程
- 现代人的学习方法
- 欢迎使用CSDN-markdown编辑器
- 面试题5:从头到尾打印链表
- 2.写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出
- 强连通分量的tarjan算法
- Android7.0新特性 新功能
- Java的接口和抽象类详解
- [LeetCode]12. Integer to Roman
- 获取当前系统时间,设置系统时间
- ListView
- OBIEE 12c Weblogic中部署自定义文件夹