天天看點

劍指offer面試題24. 反轉連結清單(雙指針)

題目描述

定義一個函數,輸入一個連結清單的頭節點,反轉該連結清單并輸出反轉後連結清單的頭節點。

劍指offer面試題24. 反轉連結清單(雙指針)

思路

詳見連結

代碼

class Solution:
	def reverseList(self, head:ListNode)->ListNode:
		if not head:
			return None
		pre = None
		cur = head
		while cur:
			cur.next, pre, cur = pre, cur, cur.next
		return pre