單鏈表的反轉

2021-10-04 00:15:41 字數 493 閱讀 2180

只需要四個指標,分別指向當前節點(b),前乙個節點(a)和後乙個節點(c)和head指標

實際上只需要三個指標,c可以用a.next表示。

步驟:節點的定義:

/**

* 單個節點的定義

* @author 追夢人

* */

public class listnode

}

反轉的函式及主函式

public class test

//前乙個

listnode pre=head;

//當前節點

listnode cur=pre.next;

while(cur!=null)

return head;

}public static void main(string args) throws interruptedexception

}}

單鏈表反轉

單鏈表反轉,可以用迴圈做,當然也可以遞迴 詳見 include includestruct node 3 1 4 6 2 1 1 3 4 6 2 2 4 1 3 6 2 3 6 4 1 3 2 4 2 6 4 1 3 5 迴圈反轉,即依次改動3個指標值,直到鍊錶反轉完成 比如,上面第 1 行到第 2...

反轉單鏈表

include stdafx.h include include using namespace std struct listnode typedef listnode plistnode typedef plistnode list list creatlist return head void...

單鏈表反轉

想起很早以前某次面試,面試官很嚴肅的要求我現場手寫單鏈表反轉的 哥虎軀一震,心想 不就需要要個臨時變數來記錄位址嗎,用得著這樣煞有介事?雖然在那之前我的確沒寫過這個程式,哈哈哈 當時我草草寫了十來行 面試官不等我完成,就直接拿過去開始問問題。不知道是不是因為抗壓能力不足,在面試官的不斷 盤問 下,哥...