链表翻转
该开始想了很久,看代码也是乱,后来发现其实就是从前到后依次翻转箭头的指向,首先把第一个表指向第二个表的箭头翻转为指向None的箭头,head指向第二个表。
current相当于交换两杯水的中间的水杯。
用于存储中间变量。
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
previous = None
while head:
current = head
head = head.next
current.next = previous
previous = current
return previous