回答編集履歴

1

while 版追記

2021/04/24 22:33

投稿

lehshell
lehshell

スコア1147

test CHANGED
@@ -93,3 +93,21 @@
93
93
  return self.head
94
94
 
95
95
  ```
96
+
97
+ 再帰より stack 使わず単純に while で繋ぎ直す方が簡単ですね。こちらも参考に提示しておきます。
98
+
99
+ Python の代入文は複数代入が可能で右辺を先に評価するため繋ぎ直し処理を4行で実装できます。
100
+
101
+ ```Python
102
+
103
+ def reverseList(self, head: ListNode) -> ListNode:
104
+
105
+ pre, cur = None, head
106
+
107
+ while cur:
108
+
109
+ cur.next, pre, cur = pre, cur, cur.next
110
+
111
+ return pre
112
+
113
+ ```