Premiumlab  

https://leetcode.com/problems/reverse-linked-list/#/description

 

Reverse a singly linked list.

 

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

 

 

Hint:

 

The key point is to make a copy of the next link of current node before point it to the previous node.

 

next = current.next

current.next = previous 

 

 

 

Sol 1 

 

 

iteratively :

# 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
        """
        current = head
        previous = None
        next = None
        while current:
            # make a copy of the next node of current before reversal 
            next = current.next
            # reverse
            current.next = previous
            # go forward in the list
            previous = current
            current = next
        return previous

 

 

Note:

 

1 Three steps to reverse a linked list in a iterative manner.

 

Step 1:  Make a copy of current.next

 

next = current.next

 

 

Step 2: Reverse. 

 

current.next = previous

 

Step 3: Forward.

 

previous = current

current = next

 

P.S. Don't forget the initialize current, previous, next = head, None, None

 

2 The iterate condition is "while current is true". 

 

3 The return value is previous. 

 

A more detailed explanation is as belows. 

 

def reverse(head):
    
    # Set up current,previous, and next nodes
    current = head
    previous = None
    nextnode = None

    # until we have gone through to the end of the list
    while current:
        
        # Make sure to copy the current nodes next node to a variable next_node
        # Before overwriting as the previous node for reversal
        nextnode = current.nextnode

        # Reverse the pointer ot the next_node
        current.nextnode = previous

        # Go one forward in the list
        previous = current
        current = nextnode

    return previous

 

 

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Linked%20Lists/Linked%20Lists%20Interview%20Problems/Linked%20List%20Interview%20Problems%20-%20SOLUTIONS/Linked%20List%20Reversal%20-%20SOLUTION.ipynb

 

 

Or think it in another way, recall "two pointer technique" in list/array section, we use "three pointer", which are previous, current, and next in order to iterate the linked list. 

 

 

 

Sol 2 :

 

Recursively 

 

# 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,previous= None):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        # current = head
        # previous= None
        # next =None
        if not head:
            return previous
        next = head.next
        head.next = previous
        return self.reverseList(next,head)

 

 

Note:

 

1 Do not change the name of "head" of the input. It is not only the name of the input variable name, but also a defined character in python linked list. 

 

2 The recursive function takes in two varibles: head(current) and previous. However, after to called function should take in : next, head. Note next and head has been switched before calling the function. 

 

3 When programming recursively, it's common to return the function at the end of the function itself (That's where recursion happens.)

 

In other cases, we may use a helper funtion to implement it. The main function is relatively simple while the helper function actually does the heavy lifting. 

 

 

 

posted on 2017-06-04 21:02  Premiumlab  阅读(104)  评论(0编辑  收藏  举报