题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
python3解法 借助reversed函数反转列表或者列表自身特性直接反转
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
head = ListNode(0)
p = head
tmp_list = []
while pHead is not None:
tmp_list.append(pHead.val)
pHead = pHead.next
# for item in tmp_list[::-1]:
for item in reversed(tmp_list):
p.next = ListNode(item)
p = p.next
return head.next