What I want is: convert a give list, e.g. a = [3, 7, 5, 8], to a singly linked list.
Node definition:
class Node(object):
def __init__(self,val):
self.val = val
self.next = None
a = [3, 7, 5, 8]
Run Code Online (Sandbox Code Playgroud)
CODE-1:
## CODE-1
cur = dummy = Node(a[0])
for e in a[1::]:
cur.next,cur = Node(e),cur.next
Run Code Online (Sandbox Code Playgroud)
CODE-2:
## CODE-2
cur = dummy = Node(a[0])
for e in a[1::]:
cur.next = Node(e)
cur = cur.next
# the following code outputs result
cur = …Run Code Online (Sandbox Code Playgroud)