Skip to content

Commit

Permalink
improve linkedlistalgo reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
victorsheng committed Mar 26, 2019
1 parent cee7915 commit 4e75d73
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions java/07_linkedlist/LinkedListAlgo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@ public class LinkedListAlgo {

// 单链表反转
public static Node reverse(Node list) {
Node headNode = null;

Node previousNode = null;
Node currentNode = list;
while (currentNode != null) {
Node nextNode = currentNode.next;
if (nextNode == null) {
headNode = currentNode;
}
currentNode.next = previousNode;
previousNode = currentNode;
currentNode = nextNode;
Node curr = list, pre = null;
while (curr != null) {
Node next = curr.next;
curr.next = pre;
pre = curr;
curr = next;
}
//我的比教程更加简洁
if (pre == null) {
return list;
} else {
return pre;
}

return headNode;
}

// 检测环
Expand Down

0 comments on commit 4e75d73

Please sign in to comment.