Skip to content

Commit edaf077

Browse files
author
chenweijie
committed
从链表开始遍历打印链表
1 parent 22598dd commit edaf077

File tree

5 files changed

+189
-4
lines changed

5 files changed

+189
-4
lines changed

src/main/java/com/chen/algorithm/exercise/ArraySearch.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.chen.algorithm.exercise;
22

33

4-
5-
64
/**
75
* 二维数组是有序的:从右上角来看,从左向右依次递减;向下数字递增;
86
* <p>
@@ -11,6 +9,7 @@
119
* 当要查找的数字比右上角的数字小时,左移;
1210
* 如果出了边界,则说明二维数组中不存在该整数。
1311
*/
12+
1413
/**
1514
* @author : chen weijie
1615
* @Date: 2019-02-20 12:53 AM
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.chen.algorithm.exercise;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
7+
*
8+
* @author : chen weijie
9+
* @Date: 2019-02-21 11:21 PM
10+
* <p>
11+
* 一种方法是利用栈来实现;
12+
* 另外一种方法是利用三个指针把链表反转,关键是 r 指针保存断开的节点。
13+
*/
14+
public class PrintLinkList {
15+
16+
public class ListNode {
17+
int val;
18+
ListNode next = null;
19+
20+
ListNode(int val) {
21+
this.val = val;
22+
}
23+
}
24+
25+
public class Solution {
26+
27+
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
28+
if (listNode == null) {
29+
return new ArrayList<>();
30+
}
31+
32+
//使用三个指针反转,
33+
ListNode head = listNode;
34+
ListNode curr = listNode.next;
35+
ListNode temp;
36+
while (curr != null) {
37+
temp = curr.next;
38+
curr.next = head;
39+
head = curr;
40+
curr = temp;
41+
}
42+
listNode.next = null;
43+
44+
//遍历
45+
ArrayList<Integer> res = new ArrayList<Integer>();
46+
while (head != null) {
47+
res.add(head.val);
48+
head = head.next;
49+
}
50+
return res;
51+
52+
53+
}
54+
}
55+
56+
57+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.chen.dataStructure.linklistnode;
2+
3+
/**
4+
* 单链表的具体实现
5+
* @author : chen weijie
6+
* @Date: 2019-02-21 11:35 PM
7+
*/
8+
public class SingleLinkedList {
9+
10+
//链表节点的个数
11+
private int size;
12+
13+
//头节点
14+
private Node head;
15+
16+
17+
18+
private class Node {
19+
20+
//每个节点的数据
21+
private Object data;
22+
23+
//每个节点指向下一个节点的连接
24+
private Node next;
25+
26+
public Node(Object data) {
27+
this.data = data;
28+
}
29+
}
30+
31+
32+
/**
33+
* 单链表的表头添加元素
34+
* @param data
35+
* @return
36+
*/
37+
public Object addHead(Object data) {
38+
39+
Node newHead = new Node(data);
40+
41+
if (size == 0) {
42+
head = newHead;
43+
} else {
44+
//新头节点的下个节点是旧的head节点
45+
newHead.next = head;
46+
//新加入的节点为SingleLinkedList的头节点
47+
head = newHead;
48+
}
49+
50+
size++;
51+
return data;
52+
}
53+
54+
55+
/**
56+
* 删除链表头节点
57+
* @return
58+
*/
59+
public Object deleteHead() {
60+
61+
Object data = head.data;
62+
63+
//新头节点为旧头节点的下个节点
64+
head = head.next;
65+
66+
size--;
67+
return data;
68+
}
69+
70+
/**
71+
* 找到指定元素返回节点Node,找不到返回null
72+
*
73+
* @param data
74+
* @return
75+
*/
76+
public Node find(Object data) {
77+
78+
Node curr = head;
79+
//只是查找,不改变链表的个数,所以要新建tempSize变量
80+
int tempSize = size;
81+
82+
while (tempSize > 0) {
83+
if (data.equals(curr.data)) {
84+
return curr;
85+
}
86+
curr = curr.next;
87+
tempSize--;
88+
}
89+
return null;
90+
}
91+
92+
/**
93+
* 删除指定元素,删除成功返回true
94+
* @param data
95+
* @return
96+
*/
97+
public boolean delete(Object data){
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
return false;
109+
}
110+
111+
112+
/**
113+
* 判断链表是不是空
114+
*
115+
* @return
116+
*/
117+
public boolean isEmpty() {
118+
return size == 0;
119+
}
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
}

src/main/java/com/chen/dataStructure/LinkNodeTest.java renamed to src/main/java/com/chen/dataStructure/linknode/LinkNodeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.chen.dataStructure;
1+
package com.chen.dataStructure.linknode;
22

33
/**
44
* http://blog.csdn.net/guyuealian/article/details/51119499(单链表的反转)

src/main/java/com/chen/dataStructure/Node.java renamed to src/main/java/com/chen/dataStructure/linknode/Node.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.chen.dataStructure;
1+
package com.chen.dataStructure.linknode;
22

33
/**
44
* @Author chenweijie

0 commit comments

Comments
 (0)