题解 | #反转链表#

反转链表

http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        pre = None
        cur = pHead
        while cur is not None: # 迭代链表
            cur_next = cur.next # 先取出当前链表后的所有结点做备份
            cur.next = pre # 设置cur这个列表的下一节点为pre节点
            pre = cur  # pre再更新为cur节点
            cur = cur_next  # 重置cur便于继续遍历
        return pre
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务