剑指offer反转链表python
题目描述
输入一个链表,反转链表后,输出新链表的表头。
思路
定义三个指针,pHead, cur,forward
反转的时候,cur.next指向pHead,然后三个指针依次向后移动,具体过程看代码。
代码
# -*- 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
if not pHead or not pHead.next:
return pHead
current = pHead.next
forward = current.next
pHead.next = None
while current:
current.next = pHead
pHead = current
current = forward
if forward:
forward = forward.next
return pHead相关推荐
koushr 2020-11-12
范范 2020-10-28
zhaochen00 2020-10-13
Mars的自语 2020-09-27
steeven 2020-09-18
kka 2020-09-14
qiangde 2020-09-13
聚沙成塔积水成渊 2020-08-16
earthhouge 2020-08-15
aanndd 2020-08-12
范范 2020-07-30
bluetears 2020-07-28
mingyunxiaohai 2020-07-19
horizonheart 2020-07-19
liushall 2020-07-18
bluetears 2020-07-05
fengshantao 2020-07-02
liuweixiao0 2020-06-27