-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
enhance swapping code in link #1660
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
Changes from 3 commits
ec56dec
bbfbf7a
2051ecb
d0c81ed
8a09815
517fc1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,22 @@ def swapNodes(self, d1, d2): | |
D1.next = D2.next | ||
D2.next = temp | ||
|
||
def swapNodes2(self, d1, d2): | ||
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. Using variable names like |
||
if d1 == d2: | ||
return | ||
else: | ||
D1 = self.head | ||
while D1 is not None and D1.data != d1: | ||
D1 = D1.next | ||
|
||
D2 = self.head | ||
while D2 is not None and D2.data != d2: | ||
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. Checking for |
||
D2 = D2.next | ||
|
||
if D1 is None or D2 is None: | ||
return | ||
|
||
D1.data, D2.data = D2.data, D1.data | ||
|
||
# swapping code ends here | ||
|
||
|
@@ -70,3 +86,7 @@ def swapNodes(self, d1, d2): | |
list.swapNodes(1, 4) | ||
print("After swapping") | ||
list.print_list() | ||
|
||
list.swapNodes2(1, 4) | ||
print("After swapping") | ||
list.print_list() |
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.
Make the method name more expressive
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.
swap_nodes_2() would be the proper Python name. See CONTRIBUTING.md. Also please add type hints to the function signature.