lintcode 35. 翻转链表

难度:容易

1. Description

lintcode 35. 翻转链表_第1张图片
35. 翻转链表

2. Solution

  • python
    时间复杂度
"""
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
        pre = None
        while(head):
            tmp = head.next
            head.next = pre
            pre = head
            head = tmp
        return pre

3. Reference

  1. https://www.lintcode.com/problem/reverse-linked-list/description

你可能感兴趣的:(lintcode 35. 翻转链表)