鍊錶反轉(C 力扣)

2021-10-23 15:55:26 字數 534 閱讀 8268

輸入乙個鍊錶的頭節點,從尾到頭反過來返回每個節點的值(用陣列返回)。

示例 1:

輸入:head = [1,3,2]

輸出:[2,3,1]

第一種方法,直接反轉

class

solution

reverse

(m.begin()

,m.end()

);return m;}}

;

由上面的**學習到,c++**的鍊錶的值,可以直接用val表示,它的值head->val,它的指向head->next;

第二種方法:

class

solution

while

(!s.

empty()

)//這個迴圈不要忘

return m;}}

;

第三種方法,利用遞迴:

class

solution

};

力扣 206反轉鍊錶

package leetcode真題分門別類.鍊錶 author bennyrhys date 2020 05 29 11 42 思路 鍊錶翻轉,直接改變指標指向 儲存狀態需要建立三個指標 pre前 cur當前 next下乙個 複雜度 時間o n 空間o 1 注意while處正好判斷cur是否為空的...

力扣 206 反轉鍊錶

反轉乙個單鏈表。示例 輸入 1 2 3 4 5 null 輸出 5 4 3 2 1 null 三個指標往後移,當 cur 指標為空時跳出迴圈 1 2 definition for singly linked list.3 public class listnode 7 8 9class soluti...

python力扣206反轉鍊錶

原題鏈結 1.雙指標反轉 非迭代法的主要思想就是設定兩個指標,指標每向前移動一下就反轉一下 class solution def reverselist self,head listnode listnode pre none cur head while cur 當cur null時迴圈結束 tm...