單鏈表逆轉

2021-09-06 17:17:51 字數 620 閱讀 2914

// p 為指向非空單鏈表中第乙個結點的指標,本演算法逆轉鍊錶並返回逆轉後的頭指標。基本思路是:如果鍊錶中只有一 個結點,則空操作,否則先逆轉a2開始的鍊錶,然後將 a1聯接到逆轉後的鍊錶的表尾(即a2)之後。 

1

//遞迴方法逆轉 單鏈表

2 node* recreverselist(node*head)38

9 node *newhead = recreverselist(head->next);

10 head->next->next =head;

11 head->next =null;

12return

newhead;13}

1415

//非遞迴方式逆轉 單鏈表

16 node* recreverselist(node*head)

1722 node* p1 =head;

23 node* p2 = p1->next;

24 head->next =null;

2526

while

(p2)

2733

return

head;

34 }

逆轉單鏈表

逆轉單鏈表 struct node reverse llist node head 此時temp表示以前的尾結點,p表示temp結點的前一結點 head temp 逆轉尾結點為頭結點 head next p 頭結點指標域指向p return head 逆轉迴圈鍊錶 struct node rever...

單鏈表逆轉

單鏈表逆轉 單鏈表逆轉,將單鏈表中各結點的next域改為指向其前驅結點。原先第乙個結點的next域為空,head改為指向原先的最後乙個結點。逆轉剛才如下圖所示 鍊錶類singlylist public class singlylist 構造單鏈表,由values陣列提供元素 public singl...

單鏈表逆轉

資料結構與演算法題目集 中文 list reverse list l 其中list結構定義如下 typedef struct node ptrtonode struct node typedef ptrtonode list 定義單鏈表型別 l是給定單鏈表,函式reverse要返回被逆轉後的鍊錶。i...