LeetCode--206--反转链表

问题描述:

反转一个单链表。

示例:

输入: 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 head
12         p = head.next
13         while p != None:
14             p = head.next
15             head.next = dummy.next
16             dummy.next = head
17             head = p
18         return dummy.next

2018-09-18 21:17:27

猜你喜欢

转载自www.cnblogs.com/NPC-assange/p/9671497.html