File tree Expand file tree Collapse file tree
src/main/java/com/crossoverjie/algorithm Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5353- [ 从一个数组中返回两个值相加等于目标值的下标] ( https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/algorithm/TwoSum.java#L38-L59 )
5454- [ 一致 Hash 算法] ( https://github.com/crossoverJie/Java-Interview/blob/master/MD/Consistent-Hash.md )
5555- [ 限流算法] ( https://github.com/crossoverJie/Java-Interview/blob/master/MD/Limiting.md )
56+ - [ 三种方式反向打印单向链表] ( https://github.com/crossoverJie/Java-Interview/blob/master/MD/Limiting.md )
5657
5758### 附加技能
5859
Original file line number Diff line number Diff line change 1+ package com .crossoverjie .algorithm ;
2+
3+ import java .util .Stack ;
4+
5+ /**
6+ * Function: 三种方式反向打印单向链表
7+ *
8+ * @author crossoverJie
9+ * Date: 10/02/2018 16:14
10+ * @since JDK 1.8
11+ */
12+ public class ReverseNode {
13+
14+
15+ /**
16+ * 利用栈的先进后出特性
17+ * @param node
18+ */
19+ public void reverseNode1 (Node node ){
20+
21+ System .out .println ("====翻转之前====" );
22+
23+ Stack <Node > stack = new Stack <>() ;
24+ while (node != null ){
25+
26+ System .out .print (node .value + "===>" );
27+
28+ stack .push (node ) ;
29+ node = node .next ;
30+ }
31+
32+ System .out .println ("" );
33+
34+ System .out .println ("====翻转之后====" );
35+ while (!stack .isEmpty ()){
36+ System .out .print (stack .pop ().value + "===>" );
37+ }
38+
39+ }
40+
41+
42+ /**
43+ * 利用头插法插入链表
44+ * @param head
45+ */
46+ public void reverseNode (Node head ) {
47+ if (head == null ) {
48+ return ;
49+ }
50+
51+ Node node ;
52+
53+ Node pre = head ;
54+ Node cur = head .next ;
55+ Node next ;
56+ while (cur != null ){
57+ next = cur .next ;
58+
59+ //链表的头插法
60+ cur .next = pre ;
61+ pre = cur ;
62+
63+ cur = next ;
64+ }
65+ head .next = null ;
66+ node = pre ;
67+
68+
69+ while (node != null ){
70+ System .out .println (node .value );
71+ node = node .next ;
72+ }
73+
74+ }
75+
76+
77+ /**
78+ * 递归
79+ * @param node
80+ */
81+ public void recNode (Node node ){
82+
83+ if (node == null ){
84+ return ;
85+ }
86+
87+ if (node .next != null ){
88+ recNode (node .next ) ;
89+ }
90+ System .out .print (node .value +"===>" );
91+ }
92+
93+
94+ public static class Node <T >{
95+ public T value ;
96+ public Node <T > next ;
97+
98+
99+ public Node (T value , Node <T > next ) {
100+ this .next = next ;
101+ this .value = value ;
102+ }
103+ }
104+ }
You can’t perform that action at this time.
0 commit comments