# -*- 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
head = pHead
if head == None:
return None
if head.next == None:
return head
if head.next.next == None:
head02 = head.next #head02为新链表的头指针
head.next.next = head
head.next = None
return head02
else:
pre = head #pre为原链表的位置指针
while pre.next != None:
if pre.next.next == None:
head02 = pre.next
pre.next = None
else:
pre = pre.next
pre = head
pre02 = head02 #pre02为新链表的位置指针
while pre.next != None:
if pre.next.next == None:
pre02.next = pre.next
pre.next = None
pre02 = pre02.next
pre = head
else:
pre = pre.next
pre02.next = head
return head02
输入一个链表,反转链表后,输出新链表的表头。python
最新推荐文章于 2022-05-23 21:59:25 发布