Practice/Google/Implement a Special Type of Linked List
Object Oriented DesignMust
Design and implement a blockchain-inspired linked list data structure. Each node in this list contains three pieces of information: a numerical value, a hash that is derived from the previous node's value, and a reference to the next node. This structure mimics simplified blockchain behavior where each block cryptographically links to its predecessor.
Your implementation should support adding new values to the chain and validating that the entire chain maintains its integrity (i.e., each node's hash correctly corresponds to the previous node's value).
Create a BlockNode class with the following attributes:
value: an integer representing the node's datahash: a string computed from the previous node's value (for the first node, use "0" as the previous hash)next: a reference to the next node in the chainCreate a BlockchainList class with the following methods:
append(value): adds a new node to the end of the chain with the given valuevalidate_chain(): returns True if the chain is valid (each node's hash matches the expected hash based on the previous node's value), False otherwiseget_values(): returns a list of all values in the chainThe hash for each node should be computed as: "hash_" + str(previous_node_value)
"hash_0" since there's no previous valueEach new node must correctly link its hash to the actual value of the previous node
Example 1:
` chain = BlockchainList() chain.append(42) chain.append(17) chain.append(99)
chain.get_values() # Returns: [42, 17, 99] chain.validate_chain() # Returns: True
Explanation:
Example 2:
` chain = BlockchainList() chain.append(100)
chain.get_values() # Returns: [100] chain.validate_chain() # Returns: True
Explanation: A single-node chain is always valid. `
Example 3:
` chain = BlockchainList() chain.validate_chain() # Returns: True
Explanation: An empty chain is considered valid. `