-
Notifications
You must be signed in to change notification settings - Fork 0
Created linked_list #6
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
WalkthroughA new file, Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant LinkedList
participant Node
User->>LinkedList: insertAtBegin(data)
LinkedList->>Node: create Node(data)
LinkedList-->>User: (insertion complete)
User->>LinkedList: insertAtIndex(data, index)
LinkedList->>Node: create Node(data)
LinkedList-->>User: (insertion complete or error)
User->>LinkedList: insertAtEnd(data)
LinkedList->>Node: create Node(data)
LinkedList-->>User: (insertion complete)
User->>LinkedList: updateNode(val, index)
LinkedList-->>User: (update or error)
User->>LinkedList: remove_first_node()
LinkedList-->>User: (removal or error)
User->>LinkedList: remove_last_node()
LinkedList-->>User: (removal or error)
User->>LinkedList: remove_at_index(index)
LinkedList-->>User: (removal or error)
User->>LinkedList: remove_node(data)
LinkedList-->>User: (removal or error)
User->>LinkedList: sizeOfLL()
LinkedList-->>User: size
User->>LinkedList: printLL()
LinkedList-->>User: (prints list)
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @visz11, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces a complete implementation of a singly linked list data structure in Python. It provides core functionalities for managing nodes, including various insertion, deletion, and update operations, along with methods to print the list and get its size.
Highlights
- Node Class: A Node class is defined to represent individual elements within the linked list, holding data and a reference to the next node.
- LinkedList Class: A LinkedList class is implemented to manage the collection of nodes, starting with a head pointer.
- Insertion Operations: Methods insertAtBegin, insertAtIndex, and insertAtEnd are provided to add new nodes at the beginning, a specific index, or the end of the list, respectively.
- Update Operation: The updateNode method allows modifying the data of a node at a given index.
- Deletion Operations: Comprehensive methods for removing nodes are included: remove_first_node, remove_last_node, remove_at_index, and remove_node (by data value).
- Utility Methods: sizeOfLL returns the number of nodes in the list, and printLL iterates through and prints the data of each node.
- Example Usage: The file includes an example script demonstrating the usage of all implemented linked list operations.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
|
/refacto-test |
🤖 Refacto.AI: PR Summary & Reviewer Guide🎯 1. The Goal (Why was this PR raised?)This PR adds a new linked list data structure implementation to the codebase. The implementation includes a complete LinkedList class with various methods for manipulating linked lists. 🗺️ 2. The Changes (What was implemented?)
🤔 3. Key Areas for Human ReviewArea of Concern: Edge Case Handling in Removal Methods
Area of Concern: Index Validation
Area of Concern: Method Consistency
|
|
Refacto is reviewing this PR. Please wait for the review comments to be posted. |
|
Your subscription expired on 30/7/2025. To continue using Refacto code reviews, please renew or upgrade your subscription. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
linked_list.py (2)
137-141: Consider improving print formatting for better readability.The current implementation prints each element on a separate line. Consider formatting the output to show the linked list structure more clearly.
def printLL(self): current_node = self.head + elements = [] while current_node: - print(current_node.data) + elements.append(str(current_node.data)) current_node = current_node.next + print(" -> ".join(elements) + " -> None")
1-179: Well-implemented linked list with comprehensive functionality.This is a solid implementation of a singly linked list data structure. The code demonstrates:
- Proper handling of all basic operations (insert, remove, update)
- Good edge case management (empty lists, invalid indices)
- Clear method organization and naming conventions
- Comprehensive example usage
Consider adding docstrings to methods for better documentation and API clarity.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
linked_list.py(1 hunks)
🔇 Additional comments (11)
linked_list.py (11)
2-5: LGTM! Clean Node class implementation.The Node class is correctly implemented with proper initialization of data and next pointer.
8-10: LGTM! Proper LinkedList initialization.The LinkedList constructor correctly initializes an empty list with head set to None.
13-16: LGTM! Correct insertion at beginning.The method properly creates a new node and updates the head pointer to maintain the linked list structure.
20-36: LGTM! Well-handled insertion at index with proper edge case management.The method correctly:
- Handles index 0 by delegating to insertAtBegin
- Traverses to find the correct position
- Handles invalid indices with appropriate error messaging
39-49: LGTM! Proper insertion at end with empty list handling.The method correctly handles both empty list initialization and traversal to the end for insertion.
52-62: LGTM! Correct node update implementation.The method properly traverses to the target index and updates the node's data with appropriate error handling for invalid indices.
65-69: LGTM! Proper first node removal.The method correctly handles empty list case and updates the head pointer appropriately.
72-86: LGTM! Comprehensive last node removal handling.The method properly handles all cases:
- Empty list
- Single node list
- Multiple nodes with correct traversal to second-to-last node
89-106: LGTM! Well-implemented removal at index.The method correctly:
- Handles empty list and index 0 cases
- Traverses to find the node before the target index
- Properly updates pointers to remove the target node
- Provides error messaging for invalid indices
109-125: LGTM! Thorough node removal by data value.The method correctly handles:
- Head node removal case
- Traversal to find matching data
- Proper pointer updates to remove the node
- Error messaging when data is not found
128-134: LGTM! Correct size calculation.The method properly traverses the entire list and returns the accurate count of nodes.
| # create a new linked list | ||
| llist = LinkedList() | ||
|
|
||
| # add nodes to the linked list | ||
| llist.insertAtEnd('a') | ||
| llist.insertAtEnd('b') | ||
| llist.insertAtBegin('c') | ||
| llist.insertAtEnd('d') | ||
| llist.insertAtIndex('g', 2) | ||
|
|
||
| # print the linked list | ||
| print("Node Data:") | ||
| llist.printLL() | ||
|
|
||
| # remove nodes from the linked list | ||
| print("\nRemove First Node:") | ||
| llist.remove_first_node() | ||
| llist.printLL() | ||
|
|
||
| print("\nRemove Last Node:") | ||
| llist.remove_last_node() | ||
| llist.printLL() | ||
|
|
||
| print("\nRemove Node at Index 1:") | ||
| llist.remove_at_index(1) | ||
| llist.printLL() | ||
|
|
||
| # print the linked list after all removals | ||
| print("\nLinked list after removing a node:") | ||
| llist.printLL() | ||
|
|
||
| print("\nUpdate node Value at Index 0:") | ||
| llist.updateNode('z', 0) | ||
| llist.printLL() | ||
|
|
||
| print("\nSize of linked list:", llist.sizeOfLL()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Move example usage behind main guard for better modularity.
The example code demonstrates the functionality well, but executing at module level prevents clean imports. Consider wrapping in a main guard.
-# create a new linked list
-llist = LinkedList()
-
-# add nodes to the linked list
-llist.insertAtEnd('a')
-llist.insertAtEnd('b')
-llist.insertAtBegin('c')
-llist.insertAtEnd('d')
-llist.insertAtIndex('g', 2)
-
-# print the linked list
-print("Node Data:")
-llist.printLL()
-
-# remove nodes from the linked list
-print("\nRemove First Node:")
-llist.remove_first_node()
-llist.printLL()
-
-print("\nRemove Last Node:")
-llist.remove_last_node()
-llist.printLL()
-
-print("\nRemove Node at Index 1:")
-llist.remove_at_index(1)
-llist.printLL()
-
-# print the linked list after all removals
-print("\nLinked list after removing a node:")
-llist.printLL()
-
-print("\nUpdate node Value at Index 0:")
-llist.updateNode('z', 0)
-llist.printLL()
-
-print("\nSize of linked list:", llist.sizeOfLL())
+if __name__ == "__main__":
+ # create a new linked list
+ llist = LinkedList()
+
+ # add nodes to the linked list
+ llist.insertAtEnd('a')
+ llist.insertAtEnd('b')
+ llist.insertAtBegin('c')
+ llist.insertAtEnd('d')
+ llist.insertAtIndex('g', 2)
+
+ # print the linked list
+ print("Node Data:")
+ llist.printLL()
+
+ # remove nodes from the linked list
+ print("\nRemove First Node:")
+ llist.remove_first_node()
+ llist.printLL()
+
+ print("\nRemove Last Node:")
+ llist.remove_last_node()
+ llist.printLL()
+
+ print("\nRemove Node at Index 1:")
+ llist.remove_at_index(1)
+ llist.printLL()
+
+ # print the linked list after all removals
+ print("\nLinked list after removing a node:")
+ llist.printLL()
+
+ print("\nUpdate node Value at Index 0:")
+ llist.updateNode('z', 0)
+ llist.printLL()
+
+ print("\nSize of linked list:", llist.sizeOfLL())📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| # create a new linked list | |
| llist = LinkedList() | |
| # add nodes to the linked list | |
| llist.insertAtEnd('a') | |
| llist.insertAtEnd('b') | |
| llist.insertAtBegin('c') | |
| llist.insertAtEnd('d') | |
| llist.insertAtIndex('g', 2) | |
| # print the linked list | |
| print("Node Data:") | |
| llist.printLL() | |
| # remove nodes from the linked list | |
| print("\nRemove First Node:") | |
| llist.remove_first_node() | |
| llist.printLL() | |
| print("\nRemove Last Node:") | |
| llist.remove_last_node() | |
| llist.printLL() | |
| print("\nRemove Node at Index 1:") | |
| llist.remove_at_index(1) | |
| llist.printLL() | |
| # print the linked list after all removals | |
| print("\nLinked list after removing a node:") | |
| llist.printLL() | |
| print("\nUpdate node Value at Index 0:") | |
| llist.updateNode('z', 0) | |
| llist.printLL() | |
| print("\nSize of linked list:", llist.sizeOfLL()) | |
| if __name__ == "__main__": | |
| # create a new linked list | |
| llist = LinkedList() | |
| # add nodes to the linked list | |
| llist.insertAtEnd('a') | |
| llist.insertAtEnd('b') | |
| llist.insertAtBegin('c') | |
| llist.insertAtEnd('d') | |
| llist.insertAtIndex('g', 2) | |
| # print the linked list | |
| print("Node Data:") | |
| llist.printLL() | |
| # remove nodes from the linked list | |
| print("\nRemove First Node:") | |
| llist.remove_first_node() | |
| llist.printLL() | |
| print("\nRemove Last Node:") | |
| llist.remove_last_node() | |
| llist.printLL() | |
| print("\nRemove Node at Index 1:") | |
| llist.remove_at_index(1) | |
| llist.printLL() | |
| # print the linked list after all removals | |
| print("\nLinked list after removing a node:") | |
| llist.printLL() | |
| print("\nUpdate node Value at Index 0:") | |
| llist.updateNode('z', 0) | |
| llist.printLL() | |
| print("\nSize of linked list:", llist.sizeOfLL()) |
🤖 Prompt for AI Agents
In linked_list.py around lines 144 to 179, the example usage code runs at the
module level, which can cause unwanted execution when importing this file. To
fix this, wrap all the example usage code inside a main guard by placing it
under if __name__ == "__main__":. This ensures the code only runs when the
script is executed directly, improving modularity and import safety.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a new Python file with a LinkedList implementation. The implementation is a good start, but it has several areas for improvement. My review focuses on making the code more robust, maintainable, and idiomatic to Python. Key suggestions include using proper exception handling instead of printing to the console, adhering to PEP 8 naming conventions, using dunder methods like __len__ and __str__, adding docstrings, and protecting the script's entry point. These changes will make the LinkedList class more reusable and easier to work with.
| # create a new linked list | ||
| llist = LinkedList() | ||
|
|
||
| # add nodes to the linked list | ||
| llist.insertAtEnd('a') | ||
| llist.insertAtEnd('b') | ||
| llist.insertAtBegin('c') | ||
| llist.insertAtEnd('d') | ||
| llist.insertAtIndex('g', 2) | ||
|
|
||
| # print the linked list | ||
| print("Node Data:") | ||
| llist.printLL() | ||
|
|
||
| # remove nodes from the linked list | ||
| print("\nRemove First Node:") | ||
| llist.remove_first_node() | ||
| llist.printLL() | ||
|
|
||
| print("\nRemove Last Node:") | ||
| llist.remove_last_node() | ||
| llist.printLL() | ||
|
|
||
| print("\nRemove Node at Index 1:") | ||
| llist.remove_at_index(1) | ||
| llist.printLL() | ||
|
|
||
| # print the linked list after all removals | ||
| print("\nLinked list after removing a node:") | ||
| llist.printLL() | ||
|
|
||
| print("\nUpdate node Value at Index 0:") | ||
| llist.updateNode('z', 0) | ||
| llist.printLL() | ||
|
|
||
| print("\nSize of linked list:", llist.sizeOfLL()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The script includes test and demonstration code at the module level. This code will execute whenever the file is imported as a module into another script, which is usually undesirable. To make this file reusable as a module, you should place this code inside an if __name__ == "__main__": block.
if __name__ == "__main__":
# create a new linked list
llist = LinkedList()
# add nodes to the linked list
llist.insertAtEnd('a')
llist.insertAtEnd('b')
llist.insertAtBegin('c')
llist.insertAtEnd('d')
llist.insertAtIndex('g', 2)
# print the linked list
print("Node Data:")
llist.printLL()
# remove nodes from the linked list
print("\nRemove First Node:")
llist.remove_first_node()
llist.printLL()
print("\nRemove Last Node:")
llist.remove_last_node()
llist.printLL()
print("\nRemove Node at Index 1:")
llist.remove_at_index(1)
llist.printLL()
# print the linked list after all removals
print("\nLinked list after removing a node:")
llist.printLL()
print("\nUpdate node Value at Index 0:")
llist.updateNode('z', 0)
llist.printLL()
print("\nSize of linked list:", llist.sizeOfLL())| def insertAtIndex(self, data, index): | ||
| if index == 0: | ||
| self.insertAtBegin(data) | ||
| return | ||
|
|
||
| position = 0 | ||
| current_node = self.head | ||
| while current_node is not None and position + 1 != index: | ||
| position += 1 | ||
| current_node = current_node.next | ||
|
|
||
| if current_node is not None: | ||
| new_node = Node(data) | ||
| new_node.next = current_node.next | ||
| current_node.next = new_node | ||
| else: | ||
| print("Index not present") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should raise an IndexError for invalid indices (out of bounds or negative) instead of printing to the console. This is more idiomatic for Python data structures and allows callers to handle errors gracefully.
| def insertAtIndex(self, data, index): | |
| if index == 0: | |
| self.insertAtBegin(data) | |
| return | |
| position = 0 | |
| current_node = self.head | |
| while current_node is not None and position + 1 != index: | |
| position += 1 | |
| current_node = current_node.next | |
| if current_node is not None: | |
| new_node = Node(data) | |
| new_node.next = current_node.next | |
| current_node.next = new_node | |
| else: | |
| print("Index not present") | |
| def insertAtIndex(self, data, index): | |
| if index < 0: | |
| raise IndexError("Index cannot be negative") | |
| if index == 0: | |
| self.insertAtBegin(data) | |
| return | |
| position = 0 | |
| current_node = self.head | |
| while current_node is not None and position + 1 != index: | |
| position += 1 | |
| current_node = current_node.next | |
| if current_node is not None: | |
| new_node = Node(data) | |
| new_node.next = current_node.next | |
| current_node.next = new_node | |
| else: | |
| raise IndexError("Index out of bounds") |
| def updateNode(self, val, index): | ||
| current_node = self.head | ||
| position = 0 | ||
| while current_node is not None and position != index: | ||
| position += 1 | ||
| current_node = current_node.next | ||
|
|
||
| if current_node is not None: | ||
| current_node.data = val | ||
| else: | ||
| print("Index not present") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should raise an IndexError for invalid indices (out of bounds or negative) instead of printing to the console. This allows callers to handle errors programmatically.
| def updateNode(self, val, index): | |
| current_node = self.head | |
| position = 0 | |
| while current_node is not None and position != index: | |
| position += 1 | |
| current_node = current_node.next | |
| if current_node is not None: | |
| current_node.data = val | |
| else: | |
| print("Index not present") | |
| def updateNode(self, val, index): | |
| if index < 0: | |
| raise IndexError("Index cannot be negative") | |
| current_node = self.head | |
| position = 0 | |
| while current_node is not None and position != index: | |
| position += 1 | |
| current_node = current_node.next | |
| if current_node is not None: | |
| current_node.data = val | |
| else: | |
| raise IndexError("Index out of bounds") |
| def remove_at_index(self, index): | ||
| if self.head is None: | ||
| return | ||
|
|
||
| if index == 0: | ||
| self.remove_first_node() | ||
| return | ||
|
|
||
| current_node = self.head | ||
| position = 0 | ||
| while current_node is not None and current_node.next is not None and position + 1 != index: | ||
| position += 1 | ||
| current_node = current_node.next | ||
|
|
||
| if current_node is not None and current_node.next is not None: | ||
| current_node.next = current_node.next.next | ||
| else: | ||
| print("Index not present") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should raise an IndexError for invalid indices (out of bounds or negative) instead of printing to the console or silently returning. This makes the behavior explicit and allows callers to handle errors.
def remove_at_index(self, index):
if index < 0:
raise IndexError("Index cannot be negative")
if self.head is None:
raise IndexError("Remove from an empty list")
if index == 0:
self.remove_first_node()
return
current_node = self.head
position = 0
# Find predecessor of the node to be deleted
while current_node.next is not None and position + 1 != index:
position += 1
current_node = current_node.next
if current_node.next is not None:
current_node.next = current_node.next.next
else:
raise IndexError("Index out of bounds")|
|
||
| # Traverse to the second last node | ||
| current_node = self.head | ||
| while current_node.next and current_node.next.next: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def remove_node(self, data): | ||
| current_node = self.head | ||
|
|
||
| # If the node to be removed is the head node | ||
| if current_node is not None and current_node.data == data: | ||
| self.remove_first_node() | ||
| return | ||
|
|
||
| # Traverse and find the node with the matching data | ||
| while current_node is not None and current_node.next is not None: | ||
| if current_node.next.data == data: | ||
| current_node.next = current_node.next.next | ||
| return | ||
| current_node = current_node.next | ||
|
|
||
| # If the data was not found | ||
| print("Node with the given data not found") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method removes the first node that matches the given data. The name and comment could be more specific about this behavior. Also, instead of printing a message when the data is not found, it would be more idiomatic to return a boolean indicating whether the removal was successful. This makes the method more versatile.
| def remove_node(self, data): | |
| current_node = self.head | |
| # If the node to be removed is the head node | |
| if current_node is not None and current_node.data == data: | |
| self.remove_first_node() | |
| return | |
| # Traverse and find the node with the matching data | |
| while current_node is not None and current_node.next is not None: | |
| if current_node.next.data == data: | |
| current_node.next = current_node.next.next | |
| return | |
| current_node = current_node.next | |
| # If the data was not found | |
| print("Node with the given data not found") | |
| # Method to remove the first node from the linked list by its data | |
| def remove_node(self, data): | |
| current_node = self.head | |
| # If the node to be removed is the head node | |
| if current_node is not None and current_node.data == data: | |
| self.remove_first_node() | |
| return True | |
| # Traverse and find the node with the matching data | |
| while current_node is not None and current_node.next is not None: | |
| if current_node.next.data == data: | |
| current_node.next = current_node.next.next | |
| return True | |
| current_node = current_node.next | |
| # If the data was not found | |
| return False |
| def sizeOfLL(self): | ||
| size = 0 | ||
| current_node = self.head | ||
| while current_node: | ||
| size += 1 | ||
| current_node = current_node.next | ||
| return size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of a custom method sizeOfLL, it's more Pythonic to implement the __len__ dunder method. This allows you to get the size of the list using the built-in len() function (e.g., len(llist)), which is more idiomatic and expected for collection-like objects in Python.
| def sizeOfLL(self): | |
| size = 0 | |
| current_node = self.head | |
| while current_node: | |
| size += 1 | |
| current_node = current_node.next | |
| return size | |
| def __len__(self): | |
| size = 0 | |
| current_node = self.head | |
| while current_node: | |
| size += 1 | |
| current_node = current_node.next | |
| return size |
| def printLL(self): | ||
| current_node = self.head | ||
| while current_node: | ||
| print(current_node.data) | ||
| current_node = current_node.next |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The printLL method directly prints to the console. A more flexible and Pythonic approach is to implement the __str__ dunder method. This method should return a string representation of the list, which can then be printed or used in other ways (e.g., print(llist)).
| def printLL(self): | |
| current_node = self.head | |
| while current_node: | |
| print(current_node.data) | |
| current_node = current_node.next | |
| def __str__(self): | |
| nodes = [] | |
| current_node = self.head | |
| while current_node: | |
| nodes.append(str(current_node.data)) | |
| current_node = current_node.next | |
| return " -> ".join(nodes) |
|
/refacto-test |
|
Refacto is reviewing this PR. Please wait for the review comments to be posted. |
Summary by CodeRabbit