Question: https://leetcode.com/problems/reverse-linked-list/
Question Name: Reverse Linked List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None: return None # Store the nodes from head to end. stack = [] while head != None: stack.append(head) head = head.next # Rebuild the list from end to head. for index in xrange(len(stack)-1, 0, -1): stack[index].next = stack[index-1] stack[0].next = None return stack[-1] |