1. How I sloved
I had to reverse the order of the linked list. I used ‘cur’ and ‘pre’ to manipulate the linked list. ‘cur’ pointed to the next node of ‘head’ and ‘pre’ pointed to the previous node of ‘head’. I repeated the process. First, ‘cur’ moved to the next node of ‘head’. Second, the next node of ‘head’ was changed to ‘pre’. Third, ‘pre’ moved to ‘head’. Finally, ‘head’ moved to ‘cur’. Before ‘pre’ moved to ‘head’, ‘head’ was returned when ‘cur’ pointed to ‘None’.
2. Code
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
cur = None
pre = None
while head:
cur = head.next
head.next = pre
if not cur:
return head
pre = head
head = cur
3. Result
Runtime : 32 ms(86.69%), Memory usage : 15.3 MB(8.03%)
(Runtime can be different by a system even if it is a same code.)