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 :: jupyter nbconvert 
Python :: python create environment linux 
Python :: get title attribute beautiful soup 
Python :: read json from api python 
Python :: pi in python math 
Python :: dataframe fill none 
Python :: python print for loop one line 
Python :: python merge list into string 
Python :: how to commenbt code in python 
Python :: python divide floor 
Python :: check if string has digits python 
Python :: how to add column to np array 
Python :: python sort dict by key 
Python :: pasal 
Python :: django drop database postgres 
Python :: case statement in querset django 
Python :: python get directory of current script file 
Python :: runtime.txt heroku python 
Python :: python file handling 
Python :: python while not 
Python :: how to print sum of two numbers in python 
Python :: python string match ignore case 
Python :: how to change the background of heading in tkinter 
Python :: how do i print a list line by line in python 
Python :: show integer seabron heatmap values 
Python :: python replace accented characters code 
Python :: python zufallszahl 
Python :: load a Dictionary from File in Python Using the Load Function of the pickle Module 
Python :: normal distribution in python 
Python :: round down python 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =