-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
create remove_duplicates.py #9330
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
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
from typing import List, Optional | ||
|
||
|
||
class Node: | ||
def __init__(self, data: int): | ||
self.data = data | ||
self.next = None | ||
|
||
|
||
def create(A: List[int]) -> Optional[Node]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
Create a linked list from an array. | ||
|
||
Args: | ||
A (List[int]): The input array. | ||
|
||
Returns: | ||
Optional[Node]: The head of the linked list. | ||
|
||
Example: | ||
>>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] | ||
>>> head = create(A) | ||
>>> display(head) | ||
1 3 5 7 9 11 15 17 19 | ||
""" | ||
if not A: | ||
return None | ||
|
||
head = Node(A[0]) | ||
last = head | ||
|
||
for data in A[1:]: | ||
last.next = Node(data) | ||
last = last.next | ||
|
||
return head | ||
|
||
|
||
def display(head: Optional[Node]) -> None: | ||
""" | ||
Display the elements of a linked list. | ||
|
||
Args: | ||
head (Optional[Node]): The head of the linked list. | ||
|
||
Example: | ||
>>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] | ||
>>> head = create(A) | ||
>>> display(head) | ||
1 3 5 7 9 11 11 15 17 19 | ||
""" | ||
while head: | ||
print(head.data, end=" ") | ||
head = head.next | ||
print() | ||
|
||
|
||
def count(head: Optional[Node]) -> int: | ||
""" | ||
Count the number of nodes in a linked list. | ||
|
||
Args: | ||
head (Optional[Node]): The head of the linked list. | ||
|
||
Returns: | ||
int: The count of nodes. | ||
|
||
Example: | ||
>>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] | ||
>>> head = create(A) | ||
>>> count(head) | ||
10 | ||
""" | ||
count = 0 | ||
while head: | ||
count += 1 | ||
head = head.next | ||
return count | ||
|
||
|
||
def delete(head: Optional[Node], index: int) -> int: | ||
""" | ||
Delete a node at a given index from a linked list. | ||
|
||
Args: | ||
head (Optional[Node]): The head of the linked list. | ||
index (int): The index at which the node should be deleted (1-based index). | ||
|
||
Returns: | ||
int: The data of the deleted node, or -1 if the index is out of range. | ||
|
||
Example: | ||
>>> A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] | ||
>>> head = create(A) | ||
>>> delete(head, 3) | ||
5 | ||
""" | ||
if index < 1 or index > count(head): | ||
return -1 | ||
|
||
if index == 1: | ||
data = head.data | ||
head = head.next | ||
return data | ||
else: | ||
prev = head | ||
current = head | ||
for _ in range(index - 1): | ||
prev = current | ||
current = current.next | ||
data = current.data | ||
prev.next = current.next | ||
return data | ||
|
||
|
||
def remove_duplicates(head: Optional[Node]) -> None: | ||
""" | ||
Remove duplicate elements from a sorted linked list. | ||
|
||
Args: | ||
head (Optional[Node]): The head of the linked list. | ||
|
||
Example: | ||
>>> A = [1, 3, 3, 5, 5, 7, 7, 9] | ||
>>> head = create(A) | ||
>>> remove_duplicates(head) | ||
>>> display(head) | ||
1 3 5 7 9 | ||
""" | ||
current = head | ||
while current and current.next: | ||
if current.data == current.next.data: | ||
current.next = current.next.next | ||
else: | ||
current = current.next | ||
|
||
|
||
if __name__ == "__main__": | ||
print("Linked List!") | ||
|
||
A = [1, 3, 5, 7, 9, 11, 11, 15, 17, 19] | ||
head = create(A) | ||
|
||
remove_duplicates(head) | ||
display(head) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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: