Leetcode #206. Reverse Linked List

Reverse a singly linked list.

一般,reverse list, reverse list by pair 等都需要三个指针,cur = head, prev, 和当前的下一个temp. 这里在head到达边界之后,还会再走一步,
所以对应新的表头的是prev.

 1 class Solution(object):
 2     def reverseList(self, head):
 3         """
 4         :type head: ListNode
 5         :rtype: ListNode
 6         """
 7 
 8         prev = None
 9 
10         while head:
11             temp = head.next
12             head.next = prev
13             prev = head
14             head = temp
15 
16         return prev

 


posted @ 2016-11-27 09:26  lettuan  阅读(194)  评论(0编辑  收藏  举报