Skip to content

Commit d17f68f

Browse files
committed
反转单链表O(n)复杂度实现
1 parent 582d450 commit d17f68f

File tree

3 files changed

+34
-229
lines changed

3 files changed

+34
-229
lines changed

src/main/java/com/algorithm/study/demo/algorithm/Jianzhi02.java

Lines changed: 0 additions & 119 deletions
This file was deleted.

src/main/java/com/algorithm/study/demo/algorithm/ZumaProject.java

Lines changed: 0 additions & 96 deletions
This file was deleted.

src/main/java/com/algorithm/study/demo/datastructure/linear/MLinkList.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
public class MLinkList<E> {
1414
// 定义一个内部类Node,代表链表的节点
1515
private class Node {
16-
private E data;// 保存数据
16+
private E value;// 保存数据
1717
private Node next;// 指向下个节点的引用
1818

1919
// 无参构造器
2020
public Node() {
2121
}
2222
// 初始化全部属性的构造器
2323
public Node(E data, Node next) {
24-
this.data = data;
24+
this.value = data;
2525
this.next = next;
2626
}
2727
}
@@ -55,7 +55,7 @@ public E deleteFirst(){
5555
data=temp.next;
5656
temp.next=null;//释放应用
5757
size--;
58-
return temp.data;
58+
return temp.value;
5959
}
6060

6161
/**
@@ -94,7 +94,7 @@ public void set(int inddex,E element){
9494
current=current.next;
9595
j++;
9696
}
97-
current.data=element;
97+
current.value=element;
9898
}
9999
/**
100100
* 增加一个末尾结点
@@ -128,7 +128,7 @@ public E get(int index){
128128
temp=temp.next;
129129
j++;
130130
}
131-
return temp.data;
131+
return temp.value;
132132
}
133133

134134
/**
@@ -160,7 +160,7 @@ public void delete(int index){
160160
public void delete(E element){
161161
Node temp=data;
162162
Node current=data;
163-
while (!temp.data.equals(element)){
163+
while (!temp.value.equals(element)){
164164
if (temp.next==null){
165165
return;
166166
}
@@ -191,24 +191,44 @@ public int size(){
191191
@Override
192192
public String toString(){
193193
StringBuilder sb=new StringBuilder();
194-
int j=1;
195194
Node temp=data;
196-
while (j<size){
197-
sb.append(data.data);
198-
temp= data.next;//找到最后一个结点
199-
j++;
195+
while (temp!=null){
196+
sb.append(temp.value);
197+
temp= temp.next;//找到最后一个结点
200198
}
201199
return sb.toString();
202200
}
201+
202+
/**
203+
* 反转链表O(n)复杂度实现
204+
*/
205+
public void reverseLinkedList(){
206+
if (data==null || data.next==null){
207+
return;
208+
}
209+
Node p1=data;
210+
Node p2=data.next;
211+
Node p3=null;
212+
while (p2!=null){
213+
p3=p2.next;
214+
p2.next=p1;
215+
p1=p2;
216+
p2=p3;
217+
}
218+
data.next=null;
219+
data=p1;
220+
System.out.println("反转完毕");
221+
}
203222
public static void main(String[] args) {
204223
MLinkList mLinkList=new MLinkList();
205224
mLinkList.add("a");
206225
mLinkList.add("b");
207226
mLinkList.add("c");
208-
mLinkList.add("b");
209-
mLinkList.add("a");
210-
System.out.println(mLinkList.toString());
227+
mLinkList.add("d");
228+
// System.out.println(mLinkList.toString());
211229
System.out.println(mLinkList.size);
230+
// System.out.println(mLinkList.toString());
231+
mLinkList.reverseLinkedList();
212232
System.out.println(mLinkList.toString());
213233

214234
}

0 commit comments

Comments
 (0)