Skip to content

Created a file revere linked list #6592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions RevrseLinkedList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# 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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: data

self.data = data
self.next = None


class LinkedList:

# Function to initialize head
def __init__(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

self.head = None

# Function to reverse the linked list
def reverse(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: reverse. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file RevrseLinkedList.py, please provide doctest for the function reverse

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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: push. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file RevrseLinkedList.py, please provide doctest for the function push

Please provide type hint for the parameter: 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):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: printList. If the function does not return a value, please provide the type hint as: def function() -> None:

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: printList

As there is no test file in this pull request nor any test function or class in the file RevrseLinkedList.py, please provide doctest for the function printList

temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next


# Driver program to test above functions
def main():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: main. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file RevrseLinkedList.py, please provide doctest for the function main

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: main. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file RevrseLinkedList.py, please provide doctest for the function main

llist = LinkedList()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)

print("Given Linked List")
llist.printList()
llist.reverse()
print("\nReversed Linked List")
llist.printList()


if __name__ == "__main__":
main()