参考解析
分析:
在原地一次翻转完成
循环head链表,将链表中的元素从表头依次取出指向新链表即可。
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: The first node of the linked list.
@return: You should return the head of the reversed linked list.
Reverse it in-place.
"""
def reverse(self, head):
# write your code here
if head is None: return None
p = head
cur = None
pre = None
while p is not None:
cur = p.next
p.next = pre
pre = p
p = cur
return pre
推荐习题
输出二叉树的最大路径和的路径。 常用的防止过拟合的技术手段有哪些, l1-norm 和 l2-norm 的区别是什 么? 给定一个正数数组arr,arr的累加和代表金条的总长度,arr的每个数代表金条要分成的长度。规定长度为k的金条分成两块,费用为k个铜板。返回把金条分出arr中的每个数字需要的最小代价。要求时间复杂度为O(nlogn),空间复杂度为O(n)。 手写一个快速排序算法 快速幂 二分查找 1.随机从某超市抽取10个顾客,得到他们购物所花费的金额(单位:元) - 数据:388,24,152,63.2,224.6,26,69,70,138,213 - 请问我们可以用哪些统计量,分析该组数据? Python中的高阶函数是指什么?