题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
# double cursor
if pHead == None or pHead.next == None:
return pHead
dummy_head = ListNode(0)
dummy_head.next = pHead
cur_1 = dummy_head
cur_2 = pHead
while cur_2:
temp = cur_2.next
cur_2.next = cur_1
cur_1 = cur_2
cur_2 = temp
dummy_head.next.next = None
return cur_1 利用双指针解决链表问题,时间复杂度为o(N),空间复杂度为o(1)