Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

reverse linked list with python

def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:

    previousNode, currentNode = None, head

    while currentNode:

        temp = currentNode.next
        currentNode.next = previousNode
        previousNode = currentNode
        currentNode = temp

    return previousNode
Comment

python reverse linked list


# Python program to reverse a linked list
# Time Complexity : O(n)
# Space Complexity : O(n) as 'next'
#variable is getting created in each loop.
 
# Node class
 
 
class Node:
 
    # Constructor to initialize the node object
    def __init__(self, data):
        self.data = data
        self.next = None
 
 
class LinkedList:
 
    # Function to initialize head
    def __init__(self):
        self.head = None
 
    # Function to reverse the linked list
    def reverse(self):
        prev = None
        current = self.head
        while(current is not None):
            next = current.next
            current.next = prev
            prev = current
            current = next
        self.head = prev
 
    # Function to insert a new node at the beginning
    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node
 
    # Utility function to print the LinkedList
    def printList(self):
        temp = self.head
        while(temp):
            print (temp.data,end=" ")
            temp = temp.next
 
 
# Driver program to test above functions
llist = LinkedList()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)
 
print ("Given Linked List")
llist.printList()
llist.reverse()
print ("
Reversed Linked List")
llist.printList()
Comment

reverse linked list python

# Python program to reverse a linked list
# Time Complexity : O(n)
# Space Complexity : O(n) as 'next' 
#variable is getting created in each loop.
  
# Node class
  
  
class Node:
  
    # Constructor to initialize the node object
    def __init__(self, data):
        self.data = data
        self.next = None
  
  
class LinkedList:
  
    # Function to initialize head
    def __init__(self):
        self.head = None
  
    # Function to reverse the linked list
    def reverse(self):
        prev = None
        current = self.head
        while(current is not None):
            next = current.next
            current.next = prev
            prev = current
            current = next
        self.head = prev
  
    # Function to insert a new node at the beginning
    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node
  
    # Utility function to print the LinkedList
    def printList(self):
        temp = self.head
        while(temp):
            print (temp.data,end=" ")
            temp = temp.next
  
  
# Driver program to test above functions
llist = LinkedList()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)
  
print ("Given Linked List")
llist.printList()
llist.reverse()
print ("
Reversed Linked List")
llist.printList()
  
Comment

PREVIOUS NEXT
Code Example
Python :: Addition/subtraction of integers and integer-arrays with DatetimeArray is no longer supported 
Python :: opencv histogram equalization 
Python :: flask marshmallow 
Python :: django import timezone 
Python :: `distplot` is a deprecated function and will be removed in a future version 
Python :: vsc python close all functions 
Python :: django import settings variables 
Python :: how to access all the elements of a matrix in python using for loop 
Python :: opencv python shrink image 
Python :: check if numpy arrays are equal 
Python :: remove trailing and leading spaces in python 
Python :: Plotting keras model trainning history 
Python :: python how to get every name in folder 
Python :: remove duplicate row in df 
Python :: except do nothing python 
Python :: tf.contrib.layers.xavier_initializer() tf2 
Python :: how to make index column as a normal column 
Python :: normalize = true pandas 
Python :: file to lowercase python 
Python :: convert period to timestamp pandas 
Python :: change text color docx-python 
Python :: create directory in python 
Python :: how to replace single string in all dictionary keys in python 
Python :: how to sort list in descending order in python 
Python :: pandas to tensor torch 
Python :: dask show progress bar 
Python :: linux command on python 
Python :: python enum declare 
Python :: command prompt pause in python 
Python :: pandas series sort 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =