Merge branch 'master' of https://github.com/byhieg/JavaTutorial · vunited/JavaTutorial@c94ffac · GitHub
Skip to content

Commit c94ffac

Browse files
committed
Merge branch 'master' of https://github.com/byhieg/JavaTutorial
2 parents 849f9f0 + d200fb2 commit c94ffac

9 files changed

Lines changed: 635 additions & 0 deletions

File tree

images/Thumbs.db

11 KB
Binary file not shown.
Lines changed: 285 additions & 0 deletions
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package cn.byhieg.algorithmtutorial;
2+
3+
/**
4+
* Created by byhieg on 17/5/2.
5+
* Mail to byhieg@gmail.com
6+
*/
7+
public class SingleLinkList {
8+
9+
10+
public Node head;
11+
12+
/**
13+
* 在当前链表尾部插入一个节点
14+
*
15+
* @param data
16+
* @return
17+
*/
18+
public Node insertFromTail(int data) {
19+
Node cur = getHead();
20+
Node node = new Node(data);
21+
if (cur == null) {
22+
head = node;
23+
return head;
24+
} else {
25+
while (cur.next != null) {
26+
cur = cur.next;
27+
}
28+
cur.next = node;
29+
}
30+
return cur;
31+
}
32+
33+
/**
34+
* 在当前链表头部插入一个节点
35+
*
36+
* @param data
37+
* @return
38+
*/
39+
public Node insertFromHead(int data) {
40+
Node node = new Node(data);
41+
node.next = head;
42+
head = node;
43+
return head;
44+
}
45+
46+
47+
public Node reverseLinkList() {
48+
if (head == null) {
49+
return head;
50+
}
51+
Node reverseHead = null;
52+
Node cur = head;
53+
Node prev = null;
54+
while (cur != null) {
55+
Node next = cur.next;
56+
if (next == null) {
57+
reverseHead = cur;
58+
}
59+
cur.next = prev;
60+
prev = cur;
61+
cur = next;
62+
}
63+
return reverseHead;
64+
}
65+
66+
/**
67+
* 打印链表
68+
*/
69+
public void printLinkList(Node head) {
70+
Node cur = head;
71+
while (cur != null) {
72+
System.out.print(cur.data + " ");
73+
cur = cur.next;
74+
}
75+
}
76+
77+
public Node getHead() {
78+
return head;
79+
}
80+
81+
public static class Node {
82+
public int data;
83+
public Node next;
84+
85+
public Node(int data) {
86+
this.data = data;
87+
}
88+
}
89+
}
Lines changed: 21 additions & 0 deletions

0 commit comments

Comments
 (0)