日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

透彻理解回文~单链表的逆序~

發布時間:2025/3/12 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 透彻理解回文~单链表的逆序~ 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

判斷一個單鏈表是不是回文,主要有三種方法,不過如果要考慮空間復雜度的話,就只有常用的一種方法了。

這種方法很考驗一個人的細心以及編程能力~

前兩種方法比較簡單我就不祥述了~

主要講一下最后一種方法:直接上圖了~



下面附上code:

public static class Node {
public int value;
public Node next;


public Node(int data) {
this.value = data;
}
}


public static void main(String args[]) {
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(2);
head.next.next.next.next.next = new Node(1);
printLinkedList(head);
isHuiWenList1(head);
isHuiWenList2(head);
isHuiWenList3(head);
}


/* 打印單鏈表 */
public static void printLinkedList(Node node) {
System.out.print("Linked List: ");
while (node != null) {
System.out.print(node.value + " ");
node = node.next;
}
System.out.println();
}


/*
* 需要額外空間復雜度O(n),這種方法在筆試中比較推薦。 大概四路如下: 新建一個堆棧,把list里面的元素一個一個????????push進入堆棧中,全部都放進去以后,
* pop出來與單鏈表中的元素取出來進行比較,如果完全相等,則說明是回文,不然不是
*/
public static boolean isHuiWenList1(Node head) {
if (head == null || head.next == null) {
return true;
}
Stack<Node> st = new Stack<Node>();
Node cur = head;
/* 將元素壓入堆棧 */
while (cur != null) {
st.push(cur);
cur = cur.next;
}
/* 判斷單鏈表中的元素是否與堆棧pop出來的元素相等 */
while (head != null) {
if (head.value != st.pop().value) {
System.out.println("false");
return false;
} else {
head = head.next;
}
}
System.out.println("true");
return true;
}


/*
* 這種方法其實不怎么推薦,雖然它額外空間復雜度是O(n/2),但是編程相對來說有點難度,不過這里涉及到一種常用的思想:
* 定義一個指針,slow和fast,slow一次走一步,fast一次走兩步,同時走,當fast走完全程的時候,slow剛好就在中點。
* 其他思想與方法一類似,也是定義一個堆棧,把單鏈表的元素放進去,再把它pop出來進行比較。
*/
public static boolean isHuiWenList2(Node head) {
Node fast = head;
Node slow = head;
if (head == null || head.next == null) {
return true;
}
/* 從這里開始走,一個走一步,一個走兩步 */
if (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
/* 下面的slow此時是中點 */
Stack<Node> st = new Stack<Node>();
while (slow != null) {
st.push(slow);
slow = slow.next;
}
while (st.size() != 0) {
if (st.pop().value != head.value) {
System.out.println("false");
return false;
} else {
head = head.next;
}
}
System.out.println("true");
return true;
}


/*
* 下面這種方法空間復雜度為o(1),適合于在筆試中使用,很考驗編程能力,主要是在逆序那里很容易出錯, 不過一旦理解了這種思想,逆序就不難理解了!
*/
public static boolean isHuiWenList3(Node head) {
if (head == null || head.next == null) {
System.out.println("true");
return true;
}


/* 找中點位置,也是通過兩個指針來實現 */
Node slow = head;
Node fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}


/* 將中點右邊的單鏈表進行逆序 */
Node n3 = null;
fast = slow.next;
slow.next = null;
while (fast != null) {
n3 = fast.next;
fast.next = slow;
slow = fast;
fast = n3;
}
/* 比較中點左邊的元素和中點右邊的元素是否相等,也即判斷是否為回文 */
n3 = slow;
fast = head;
boolean res = true;
while (slow != null && fast != null) {
if (slow.value != fast.value) {
res = false;
break;
}
slow = slow.next;
fast = fast.next;
}
/* 將中點右邊逆序的部分恢復過來,其實這里我也不太懂為什么要恢復過來,歡迎各位大神能來解答一下 */
slow = n3.next;
n3.next = null;
while (slow != null) {
fast = slow.next;
slow.next = n3;
n3 = slow;
slow = fast;
}
System.out.println(res);
return res;
}

總結

以上是生活随笔為你收集整理的透彻理解回文~单链表的逆序~的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。