Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self,data):
- self.data=data
- self.next= None
- class linked_list:
- def __init__(self):
- self.head = None
- def print_node(self):
- current_node=self.head
- while current_node:
- print(current_node.data)
- current_node=current_node.next
- def append(self,data):
- new_node=Node(data)
- if self.head is None:
- self.head=new_node
- return
- last_node=self.head
- while last_node.next:
- last_node=last_node.next
- last_node.next=new_node
- def swap_nodes(self,key1,key2):
- if key1==key2:
- return
- previous_node1=None
- current_node1=self.head
- while current_node1 and current_node1!= key1:
- previous_node1=current_node1
- current_node1=current_node1.next
- previous_node2= None
- current_node2=self.head
- while current_node2 and current_node2!=key2:
- previous_node2=current_node2
- current_node2=current_node2.next
- if not current_node1 or not current_node2:
- return
- if previous_node1:
- previous_node1.next=current_node2
- else:
- self.head=current_node2
- if previous_node2:
- previous_node2.next=current_node1
- else:
- self.head=current_node1
- current_node1.next,current_node2.next=current_node2.next,current_node1.next
- llistt=linked_list()
- llistt.append('a')
- llistt.append("b")
- llistt.append('c')
- llistt.append("d")
- #llistt.delete_middle_node("b")
- #llistt.print_node()
- #print(llistt.len_iterative())
- llistt.swap_nodes("a","b")
- llistt.print_node()
Add Comment
Please, Sign In to add comment