"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: n
@return: The new head of reversed linked list.
"""
def reverse(self, head):
# write your code here
if head == None:
return None
if head.next == None:
return head
ptr = head
ptr2 = head.next
head.next = None
ptr3 = ptr2.next
while ptr3 != None:
ptr2.next = ptr
ptr = ptr2
ptr2 = ptr3
ptr3 = ptr3.next
ptr2.next = ptr
return ptr2
Python, LintCode, 35. 翻转链表
最新推荐文章于 2024-12-26 14:55:02 发布