LeetCode--206--反转链表

问题描述:

反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

方法1:头插法 新增头结点

2018-09-18 21:17:27

 1 class Solution:
 2     def reverseList(self, head: ListNode) -> ListNode:
 3         dummy=ListNode(-1)
 4         dummy.next=None
 5         p = head
 6         while p:
 7             q=p.next
 8             p.next=dummy.next
 9             dummy.next=p
10             p=q
11         return dummy.next
 1 class Solution:
 2     def reverseList(self, head: ListNode) -> ListNode:
 3         if head==None:
 4             return head
 5         p=head.next
 6         head.next=None
 7         while p:
 8             q = p.next
 9             p.next=head
10             head=p
11             p=q
12         return head
13             

2019-12-28 10:01:45

原文地址:https://www.cnblogs.com/NPC-assange/p/9671497.html