Created
August 30, 2017 11:29
-
-
Save Rand01ph/1c3306a65e8d0d4f1ed76fef94852e08 to your computer and use it in GitHub Desktop.
A LinkedList Problem
This file contains 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
class Node: | |
def __init__(self, data): | |
self.data = data | |
self.next = None | |
class LinkedList: | |
def __init__(self): | |
self.head = None | |
self.tail = None | |
def r_append(self, data): | |
node = Node(data) | |
if self.tail is None: | |
self.head = node | |
self.tail = node | |
else: | |
node.next = self.head | |
self.head = node | |
def iter(self): | |
if not self.head: | |
return | |
cur = self.head | |
yield cur.data | |
while cur.next: | |
cur = cur.next | |
yield cur.data | |
if __name__ == '__main__': | |
linked_list = LinkedList() | |
d = {} | |
list_node = raw_input("eg: 3->1\n") | |
while list_node != '': | |
input_list = list_node.split('->') | |
if input_list[1] == 'null': | |
linked_list.r_append(input_list[0]) | |
else: | |
d[input_list[1]] = input_list[0] | |
list_node = raw_input("eg: 3->1\n") | |
tail = linked_list.tail.data | |
while d: | |
linked_list.r_append(d[tail]) | |
del d[tail] | |
tail = linked_list.head.data | |
for i in linked_list.iter(): | |
print i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment