package chapter_2_listproblem;
public class Problem_04_ReverseList {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
public static Node reverseList(Node head) {
Node pre = null;
Node next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
public static class DoubleNode {
public int value;
public DoubleNode last;
public DoubleNode next;
public DoubleNode(int data) {
this.value = data;
}
}
public static DoubleNode reverseList(DoubleNode head) {
DoubleNode pre = null;
DoubleNode next = null;
while (head != null) {
next = head.next;
head.next = pre;
head.last = next;
pre = head;
head = next;
}
return pre;
}
public static void printLinkedList(Node head) {
System.out.print("Linked List: ");
while (head != null) {
System.out.print(head.value + " ");
head = head.next;
}
System.out.println();
}
public static void printDoubleLinkedList(DoubleNode head) {
System.out.print("Double Linked List: ");
DoubleNode end = null;
while (head != null) {
System.out.print(head.value + " ");
end = head;
head = head.next;
}
System.out.print("| ");
while (end != null) {
System.out.print(end.value + " ");
end = end.last;
}
System.out.println();
}
public static void main(String[] args) {
Node head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
printLinkedList(head1);
head1 = reverseList(head1);
printLinkedList(head1);
DoubleNode head2 = new DoubleNode(1);
head2.next = new DoubleNode(2);
head2.next.last = head2;
head2.next.next = new DoubleNode(3);
head2.next.next.last = head2.next;
head2.next.next.next = new DoubleNode(4);
head2.next.next.next.last = head2.next.next;
printDoubleLinkedList(head2);
printDoubleLinkedList(reverseList(head2));
}
}
Problem_04_ReverseList
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén)司光,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人悉患,你說(shuō)我怎么就攤上這事残家。” “怎么了售躁?”我有些...
- 文/不壞的土叔 我叫張陵坞淮,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我陪捷,道長(zhǎng)回窘,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任揩局,我火速辦了婚禮毫玖,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘凌盯。我一直安慰自己付枫,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布驰怎。 她就那樣靜靜地躺著阐滩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪县忌。 梳的紋絲不亂的頭發(fā)上掂榔,一...
- 那天,我揣著相機(jī)與錄音症杏,去河邊找鬼装获。 笑死,一個(gè)胖子當(dāng)著我的面吹牛厉颤,可吹牛的內(nèi)容都是我干的穴豫。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼逼友,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼精肃!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起帜乞,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤司抱,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后黎烈,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體习柠,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡匀谣,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了津畸。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片振定。...
- 正文 年R本政府宣布,位于F島的核電站膏执,受9級(jí)特大地震影響驻售,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜更米,卻給世界環(huán)境...
- 文/蒙蒙 一欺栗、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧征峦,春花似錦迟几、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至蛉加,卻和暖如春蚜枢,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背针饥。 一陣腳步聲響...
- 正文 我出身青樓筷凤,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親户盯。 傳聞我的和親對(duì)象是個(gè)殘疾皇子嵌施,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 從本篇文章/音頻/視頻中我學(xué)到的最重要的概念 在人生中所遇的各種各樣的事情中,不過(guò)只有三步而已罷了莽鸭,一發(fā)現(xiàn)問(wèn)題吗伤,二...
- 本文介紹停機(jī)問(wèn)題,網(wǎng)上有一些證明硫眨,但是細(xì)節(jié)方面有點(diǎn)小漏洞足淆,我們這里優(yōu)化了一下巢块。 停機(jī)問(wèn)題:是否存在一個(gè)確定的程序(...
- Day 12 神句文檔 The team contends that these bear more than a...
- 《迪森安全俠》劉金: ????"永安行動(dòng)"號(hào)族奢! ? 開(kāi)啟秋季 第三周 9月22日 安全之路,任重道遠(yuǎn)迪森之家丹鸿,與您...