實現單鏈表的逆置或叫翻轉

2021-07-03 14:55:30 字數 705 閱讀 2541

鍊錶的問題一直不能很輕鬆地想明白,其實主要是理解鍊錶結點的結構,以及他是如何進行遍歷和指標變換的。

單鏈表的逆置,需要使用額外的三個指標,分別指向當前遍歷到的結點、他的前乙個結點,以及他的後乙個結點。分別定義為 current,pre,pnext;這三個指標同時進行鍊錶的下行,直到第三個指標指向表尾null;最後返回最後乙個結點的位置;

下面是具體**,請作參考:

// reverselistnode.cpp : 定義控制台應用程式的入口點。

/* @mishidemudong

@2015-7-4-14:50

*///

#include "stdafx.h"

#include#includeusing namespace std;

struct listnode

;listnode* reverselist(listnode* phead)

return reverselistnode;

}void printlist(listnode* phead)

cout << endl; }}

listnode *createlist()

current->m_pnext = null;

return phead;

}int _tmain(int argc, _tchar* argv)

單鏈表逆置

單鏈表逆置 include include define item num 10 typedef struct tagnode node node linklist create void linklist destroy node head void linklist print node hea...

單鏈表逆置

name 單鏈表逆置 author 巧若拙 date 22 11 14 16 13 description 分別用遞迴和非遞迴兩種方式實現單鏈表 不含頭結點 的逆置 include include include typedef char elemtype typedef int status 函式...

單鏈表逆置

最近在leetcode oj上刷題,將一些演算法題的解法記錄下來,也期待一些新的更好的方法。題目是這樣滴 206.reverse linked list reverse a singly linked list.hint a linked list can be reversed either it...