# encoding=utf-8 class LNode(object): def __init__(self, x): self.data = x self.next = None def reverse(head): if head is None or head.next is None: return cur = None next = None cur = head.next.next head.next.next = None while cur: next = cur.next c…
链表反转是链表相关问题最基础的知识,做完LeetCode中LinkedList后才会有这种体会,因为ACM算法中不会涉及这一部分.解决这一问题有多种方法,在面试中面试官通常也会要求写出多种.包括stack,iterative,以及recursive. (1) stack: a. 按顺序将节点push到stack中,next赋为NULL: b. 从stack中取节点依次链接. 但是开了额外的空间,来不及多解释了,看下一个吧. (2) iterative: a. 创建根节点root; b. 依次将每…