微软算法100题58 从尾到头输出链表(java)
题目:输入一个链表的头结点,从尾到头反过来输出每个结点的值
以前做过相似的,可以用递归解决,也可以用非递归解决
1 package com.rui.microsoft; 2 3 import java.util.Stack; 4 5 public class Test_LinkedList { 6 7 public static void main(String[] args) { 8 Node<Integer> head = new Node<Integer>(0); 9 10 Node<Integer> node1 = new Node<Integer>(1); 11 Node<Integer> node2 = new Node<Integer>(2); 12 Node<Integer> node3 = new Node<Integer>(3); 13 14 head.next = node1; 15 node1.next = node2; 16 node2.next = node3; 17 18 Test_LinkedList app = new Test_LinkedList(); 19 20 //app.removeNodeWithO1(head, node3); 21 //app.print(head); 22 23 //Node<Integer> last = app.reverseRec(head); 24 //app.print(last); 25 26 Node<Integer> newHead = app.reverseNoRec2(head); 27 app.print(newHead); 28 } 29 30 //递归逆转链表 31 Node<Integer> reverseRec(Node<Integer> head){ 32 if(null == head) return head; 33 if(null == head.next) return head; 34 35 Node<Integer> last = reverseRec(head.next); 36 head.next = null; 37 last.next = head; 38 return head; 39 } 40 41 //非递归逆转链表 better 42 Node<Integer> reverseNoRec2(Node<Integer> head){ 43 if(null == head) return head; 44 if(null == head.next) return head; 45 46 Node<Integer> current = head.next; 47 Node<Integer> prev = head; 48 49 while(null != current){ 50 prev.next = current.next; 51 current.next = head; 52 head = current; 53 current = prev.next; 54 } 55 return head; 56 } 57 58 //非递归逆转链表 59 Node<Integer> reverseNoRec1(Node<Integer> head){ 60 if(null == head) return head; 61 if(null == head.next) return head; 62 63 Stack<Node<Integer>> stack = new Stack<Node<Integer>>(); 64 65 Node<Integer> node = head; 66 while(null!=node){ 67 stack.push(node); 68 node = node.next; 69 } 70 71 Node<Integer> newHead = stack.pop(); 72 Node tmp = newHead; 73 while(!stack.isEmpty()){ 74 tmp.next = stack.pop(); 75 tmp = tmp.next; 76 } 77 tmp.next = null; 78 return newHead; 79 } 80 81 //在O(1)时间内删除单向链表结点 82 void removeNodeWithO1(Node<Integer> head, Node<Integer> del){ 83 //If not tail 84 if(del.next != null){ 85 Node<Integer> temp = del.next; 86 del.value = temp.value; 87 del.next = temp.next; 88 temp = null; 89 } 90 else{ 91 Node node = head; 92 Node prev = head; 93 while(node != del){ 94 prev = node; 95 node = node.next; 96 } 97 98 prev.next = null; 99 del = null; 100 } 101 } 102 103 private void print(Node node){ 104 while(node != null){ 105 System.out.print(" " + node.value ); 106 node = node.next; 107 } 108 } 109 } 110 111 class Node<T>{ 112 T value; 113 Node<T> next; 114 115 public Node(T t){ 116 value = t; 117 } 118 }
转载于:https://www.cnblogs.com/aalex/p/5019222.html
微软算法100题58 从尾到头输出链表(java)相关推荐
- 程序员面试题精选100题(31)-从尾到头输出链表[数据结构]
题目:输入一个链表的头结点,从尾到头反过来输出每个结点的值.链表结点定义如下: struct ListNode {int m_nKey;ListNode* m_pNext; }; 分析:这是一道很有意 ...
- < 每日算法 - JavaScript解析:从尾到头打印链表 >
每日算法 - JavaScript解析:从尾到头打印链表 一.任务描述: > 示例一: 二.题意解析 拓展知识 三.解决方案: 往期内容
- 《LeetCode力扣练习》剑指 Offer 06. 从尾到头打印链表 Java
<LeetCode力扣练习>剑指 Offer 06. 从尾到头打印链表 Java 一.资源 题目: 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回). 示例 1: 输入: ...
- 剑指offer_第3题_从尾到头打印链表
题目描述 输入一个链表: 按链表值从尾到头的顺序返回一个ArrayList. 链表结构 class ListNode:def __init__(self, x):self.val = xself.ne ...
- 微软算法100题11 求二叉树中两节点之间的最大距离
第11 题 求二叉树中节点的最大距离... 如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的, 我们姑且定义"距离"为两节点之间边的个数. 写一个程序, 求一棵二叉树中相 ...
- 微软算法100题26 左旋转字符串
26.左旋转字符串 题目: 定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部. 如把字符串abcdef 左旋转2 位得到字符串cdefab.请实现字符串左旋转的函数. 要求时间对长度 ...
- LeetCode -剑指Offer 06 - 从尾到头打印链表 - java - 细喔
文章目录 题目 题目解析 解题思维 代码如下 代码细节 - 逆序 附加内容 题目 题目解析 题目目的很直接!就是想让你从链表尾结点开始到头节点结束,将每个节点的val 装入一个数组里,并将其返回. ...
- 微软面试100题(含全部答案)
引言 无私分享造就开源的辉煌. 今是二零一一年十月十三日,明日14日即是本人刚好开博一周年.在一周年之际,特此分享出微软面试全部100题答案的完整版,以作为对本博客所有读者的回馈. 一年之前的10月1 ...
- 微软面试100题2010年版全部答案集锦
微软等数据结构+算法面试100题全部答案集锦 作者:July.阿财. 时间:二零一一年十月十三日. 引言 无私分享造就开源的辉煌. 今是二零一一年十月十三日,明日14日即是本人刚好开博一周年.在一 ...
最新文章
- JavaScript PHP模仿C#中string.format效果
- java中xpath_java-xpath学习
- MVC3学习 六 HtmlHelper的使用与扩展
- java轻功游戏,会轻功又可以飞的游戏(3d大型游戏)
- MSP432P401R TI Drivers 库函数学习笔记(八)ADC
- struts2值栈分析
- 应用案例 | 从Storm到Flink,有赞五年实时计算效率提升实践
- 通向架构师的道路(第八天)之weblogic与apache的整合与调优 转
- git sync fatal: Authentication failed for https://github.com/ did not exit cleanly (exit code 128)
- 【吐血推荐】什么是领域驱动设计?DDD?
- Power bi 3.12 瀑布图
- Address already in use: JVM_Bind:8080 关于XXX端口被占用问题的解决
- Python序列 数据类型 创建方式 Tuple元组 Str字符串 List列表 dict字典 Set集合 range,zip,map,enumerate
- NVIDIA CUDA 高度并行处理器编程(九):并行模式:稀疏矩阵-向量乘法
- HDU6441(费马大定理)
- excel在双显示器上打开两个独立的xlsx表格
- 大数据处理过程之核心技术ETL详解
- uniapp 的多选框传值
- 关联规则(Association Rules)
- 搜索引擎的设计与实现(一)从零开始?
热门文章
- 与word2vec_NLP--Word2Vec详解
- java读取文件替换字符,跳槽薪资翻倍
- 【Java Web开发指南】线程安全和单线程
- python【蓝桥杯vip练习题库】ALGO-195 1的个数
- python【蓝桥杯vip练习题库】BASIC-21Sine之舞(递归 递推)
- 专注年轻人的AI学习平台
- 搭建Ubuntu18.04+Anaconda3.x+Pycharm+SimpleITK(二)
- JAVA另类_java stream的几种另类用法
- 怎么做网络推广浅析有关404页面优化的技巧
- 网络推广营销浅析网站度过“沙盒期”后,为什么还不收录?