Skip to content

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

Merged
merged 6 commits into from
Jan 14, 2020
Merged
Changes from 1 commit
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
69 changes: 15 additions & 54 deletions data_structures/linked_list/swap_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ def __init__(self):
def print_list(self):
temp = self.head
while temp is not None:
print(temp.data)
print(temp.data, end=' ')
temp = temp.next
print()

# adding nodes
def push(self, new_data):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Needs type hints and more pythonic condition checking

Copy link
Member

Choose a reason for hiding this comment

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

Do we want to limit the data type? Currently this can be used with many different data types and I consider that a feature, not a bug.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then let's be consistent by using typing.Any

Expand All @@ -21,72 +22,32 @@ def push(self, new_data):
self.head = new_node

# swapping nodes
def swapNodes(self, d1, d2):
prevD1 = None
prevD2 = None
if d1 == d2:
def swap_nodes(self, node_data_1, node_data_2):
if node_data_1 == node_data_2:
return
else:
# find d1
D1 = self.head
while D1 is not None and D1.data != d1:
prevD1 = D1
D1 = D1.next
# find d2
D2 = self.head
while D2 is not None and D2.data != d2:
prevD2 = D2
D2 = D2.next
if D1 is None and D2 is None:
return
# if D1 is head
if prevD1 is not None:
prevD1.next = D2
else:
self.head = D2
# if D2 is head
if prevD2 is not None:
prevD2.next = D1
else:
self.head = D1
temp = D1.next
D1.next = D2.next
D2.next = temp

def swapNodes2(self, d1, d2):
if d1 == d2:
return
else:
D1 = self.head
while D1 is not None and D1.data != d1:
D1 = D1.next
node_1 = self.head
while node_1 is not None and node_1.data != node_data_1:
node_1 = node_1.next

D2 = self.head
while D2 is not None and D2.data != d2:
D2 = D2.next
node_2 = self.head
while node_2 is not None and node_2.data != node_data_2:
node_2 = node_2.next

if D1 is None or D2 is None:
if node_1 is None or node_2 is None:
return

D1.data, D2.data = D2.data, D1.data
node_1.data, node_2.data = node_2.data, node_1.data

# swapping code ends here


if __name__ == "__main__":
list = Linkedlist()
Copy link
Member

Choose a reason for hiding this comment

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

list is the name of a Python builtin so it should not be used as a variable name.

Copy link
Collaborator

@onlinejudge95 onlinejudge95 Jan 13, 2020

Choose a reason for hiding this comment

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

Also according to PEP8 should the class names be LinkedList instead of Linkedlist

list.push(5)
list.push(4)
list.push(3)
list.push(2)
list.push(1)
for i in range(5, 0, -1):
list.push(i)

list.print_list()

list.swapNodes(1, 4)
print("After swapping")
list.print_list()

list.swapNodes2(1, 4)
list.swap_nodes(1, 4)
print("After swapping")
list.print_list()