反转链表

链表中倒数第k个结点

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

直接反转

class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        pre=None # 代表刚刚走过的节点
        while pHead: #为空跳出,刚刚走过的节点pre就是末节点
            next=pHead.next  #取出next
            pHead.next=pre   #将当前节点的next换为上个节点pre
            pre=pHead        #换完之后当前节点走过,pre更新
            pHead=next         #当前节点更新
        return pre
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务