问题描述:
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL
方法1:头插法
1 class Solution(object): 2 def reverseList(self, head): 3 """ 4 :type head: ListNode 5 :rtype: ListNode 6 """ 7 dummy = ListNode(-1) 8 if head == None: 9 return []10 if head.next == None:11 return head12 p = head.next13 while p != None:14 p = head.next15 head.next = dummy.next16 dummy.next = head17 head = p18 return dummy.next
2018-09-18 21:17:27