LeetCode 206. 反转链表

时间复杂度 O(n)

Java
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
while (head != null){
ListNode temp = head.next;
head.next = prev;
prev = head;
head = temp;
}
return prev;
}
}

//runtime:0 ms
//memory:38.3 MB

Python
class Solution:
def reverseList(self, head):
cur, prev = head, None
while cur:
cur.next, prev, cur = prev, cur, cur.next
return prev
# runtime:52 ms
# memory:7.7 MB