共計 3708 個字符,預計需要花費 10 分鐘才能閱讀完成。
這篇文章主要講解了“Java 怎么實現鏈表的反轉”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“Java 怎么實現鏈表的反轉”吧!
import java.util.ArrayList;
import java.util.Stack;
/**
* 鏈表的反轉
*/
public class ReverseLinkedList {
* 非遞歸地反轉鏈表: *
* 特點:沒有進行節點間的兩兩反轉,而是采用依次插入到新鏈表的方式來實現反轉。 *
* 過程:遍歷一次鏈表,將遍歷的節點依次插入到新鏈表的頭部即可實現鏈表的反轉。 *
* 復雜度:時間復雜度為 O(N), 輔助的空間復雜度為 O(1)
*
* [@param](https://my.oschina.net/u/2303379) head
*
* [@return](https://my.oschina.net/u/556800) 將反轉后的新鏈表的頭節點返回。 */
public static Node reverseByInsertToNewList(Node head) {
Node current = head; // 正在遍歷的節點
Node next = null; // 下一個要遍歷的節點
Node newHead = null; // 新鏈表頭節點的引用(指向新鏈表頭節點的指針)
while (null != current) {
next = current.next; // 將下一個要遍歷的節點暫存起來
/**
* 將當前節點放到新鏈表的頭部: * 1 將當前節點指向新鏈表的頭部
* 2 更新 newHead
*/
current.next = newHead;
newHead = current;
current = next; // 向后繼續遍歷
}
return newHead;
* 注意: * 1)最后一個節點沒有后繼節點,故最后一個節點不需要進行反轉操作,只需將最后一個節點 (作為新鏈表的頭節點) 直接返回即可。 * 2)當前節點為鏈表倒數第二個節點時,開始第一次的反轉操作。 * 3)每次的反轉操作都不會對新鏈表的頭節點造成影響,只是將新的頭結點返回而已。 *
* 解析: * 1)將 A- B- C- D 反轉的操作可以分為: * 1 將 C- D 反轉 == A- B- C D- C- null
* 2 將 B- C 反轉 == A- B D- C- B- null
* 3 將 A- B 反轉 == D- C- B- A- null
*
* 2)將 A- B 反轉的操作: * 1 將 B 的后繼節點指向 A, 即 B.next = A 即 A.next.next = A
* 2 將 A 的后繼節點設為 null, 即 A.next = null
*
* [@param](https://my.oschina.net/u/2303379) current
*
* [@return](https://my.oschina.net/u/556800) 將反轉后的新鏈表的頭節點返回。 */
private static Node reverseByRecursion(Node current) { if (null == current) { // 若該鏈表為空鏈表,則直接返回
return current;
}
if (null == current.next) { // 當前結點是尾結點時,直接返回。注:鏈表的尾節點即新鏈表的頭節點
return current;
}
Node newHead = reverseByRecursion(current.next); // newHead 是鏈表的尾節點,即新鏈表的頭節點。 // 反轉操作:將 current 和 current.next 進行反轉,即:將當前節點放到新鏈表的尾部,故在遞歸的過程中新鏈表的頭部不會變。 current.next.next = current;
current.next = null;
// 將新鏈表的頭節點返回。 return newHead;
* [@param](https://my.oschina.net/u/2303379) head
* @return 將反轉后的新鏈表的頭節點返回。 */
public static Node reverseWithStack(Node head) {
Stack Node stack = new Stack Node
while (null != head) { stack.push(head);
head = head.next;
}
return stack.peek();
*/
public static Node reverseBidirectionalList(Node head) { if (null == head) { // 若該鏈表為空鏈表,則直接返回
return null;
}
Node current = head;
Node next = null;
Node newHead = null;
while (null != current) {
// 反轉
next = current.next; // 將當前節點的后繼節點暫存起來
current.next = current.prev;
current.prev = next;
if (null == next) { // 若下一個要遍歷的節點為 null,則說明已經到達鏈表的尾部,此時 current 即鏈表的尾節點
newHead = current;
}
current = next; // 繼續向后遍歷
}
return newHead;
ArrayList Object list = new ArrayList ();
while (null != head.next) { list.add(head.value);
head = head.next;
}
list.add(head.value);
System.out.println(list.toString());
* 將雙向鏈表從尾到頭依次打印出來
*
* @param tail
*/
public static void printBidirectionList(Node tail) { ArrayList Object list = new ArrayList ();
while (null != tail.prev) { list.add(tail.value);
tail = tail.prev;
}
list.add(tail.value);
System.out.println(list.toString());
* 初始化鏈表
*
* @return
*/
public static Node init() { Node head = new Node(5);
Node node1 = new Node(3);
Node node2 = new Node(2);
Node node3 = new Node(6);
Node node4 = new Node(7);
Node node5 = new Node(17);
Node node6 = new Node(9);
head.next = node1;
node1.prev = head;
node1.next = node2;
node2.prev = node1;
node2.next = node3;
node3.prev = node2;
node3.next = node4;
node4.prev = node3;
node4.next = node5;
node5.prev = node4;
node5.next = node6;
node6.prev = node5;
node6.next = null;
return head;
// 反轉單向鏈表
// Node newHead = reverseByInsertToNewList(head);
// Node newHead = reverseByRecursion(head);
// 反轉雙向鏈表
Node newHead = reverseBidirectionalList(head);
print(newHead);
// 利用 stack 反轉雙向鏈表
Node newHeadByStack = reverseWithStack(head);
printBidirectionList(newHeadByStack);
}
感謝各位的閱讀,以上就是“Java 怎么實現鏈表的反轉”的內容了,經過本文的學習后,相信大家對 Java 怎么實現鏈表的反轉這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!
正文完