Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

reverse linked list

def reverseLinkedList(self):
        # Complexity Time: O(n), Complexity Memory: O(1)
        # Initialize previous and current pointers
        prev, curr = None, self.head
        # loop over the linked list nodes until the current is Null 
        while curr:
        	# Store current's next node in a temporary variable 
            next = curr.next
            # Reverse current node's pointer
            curr.next = prev
            # Move pointers one position ahead
            prev = curr
            curr = next
        self.head = prev
Source by leetcode.com #
 
PREVIOUS NEXT
Tagged: #reverse #linked #list
ADD COMMENT
Topic
Name
8+9 =