反轉單鏈表

2021-08-07 18:32:37 字數 606 閱讀 4692

csdn第一篇部落格,之前的部落格一直放在自己的**上,可是現在覺得wordpress好不方便,還是在csdn上寫吧,從現在開始記錄找工作的點點滴滴。

反轉單鏈表,超級熱門的面試題,反正我是看到不下十次了,有遞迴和非遞迴兩種方法

第一種:非遞迴方法

#include #include#includeusing namespace std;

/*鍊錶結構體*/

struct node;

static void reverse(struct node** head_ref)

*head_ref=prev;

}void push(struct node** head_ref,int new_data)

void printlist(struct node *head)

}int main()

{ struct node *head=null;

push(&head,2);

push(&head,4);

push(&head,6);

push(&head,8);

printlist(head);

reverse(&head);

cout<

單鏈表反轉

單鏈表反轉,可以用迴圈做,當然也可以遞迴 詳見 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...

單鏈表反轉

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