Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self, value, next=None):
- self.value = value
- self.next = next
- def __str__(self):
- curr = self
- output = ''
- while curr:
- output += str(curr.value)
- curr = curr.next
- return output
- def list_to_linkedlist(list):
- n = Node(list[0])
- head = n
- for i in list[1:]:
- n.next = Node(i)
- n = n.next
- return head
- def delete_node(node,value):
- current = node
- prev = None
- next = current.next
- while current.next:
- if current.value != value:
- prev = current
- current = current.next
- next = current.next
- else:
- prev.next = current.next
- break
- return node
- def create_odd_even_ll(node):
- index = 0
- ell = None
- oll = None
- while node:
- if index%2 == 0 and not ell:
- ell = Node(node.value)
- ell_head = ell
- elif index%2 != 0 and not oll:
- oll = Node(node.value)
- oll_head = oll
- elif index%2 == 0 and ell:
- ell.next = Node(node.value)
- ell = ell.next
- elif index%2 != 0 and oll:
- oll.next = Node(node.value)
- oll = oll.next
- index += 1
- node = node.next
- ell.next = oll_head
- return ell_head
- if __name__ == "__main__":
- list = [1,2,3,4,5,6]
- x = list_to_linkedlist(list)
- print(create_odd_even_ll(x))
- ➜ TechDev python linked_list.py
- 135246
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement