Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

python - Why do my program prints None in the end?

I'm learning algorithms and data structures by my own and want to write a program that merges two linked lists in the following way: given two integers a and b it will remove nodes from a-th to b-th from the first linked list and insert second linked list in their place.

I took an implementation of linked list from here and wrote a function

def merge(list1: LinkedList, a: int, b: int, list2: LinkedList) -> LinkedList:

    current = list1.head
    previous = None
    insertion = list2.head
    counter = 0

    while insertion.next_node:
        insertion = insertion.next_node

    while current:
        if counter == a:
            previous.next_node = list2.head
        elif counter == b:
            insertion.next_node = current.next_node
        previous = current
        counter += 1
        current = current.next_node

    return list1.printList()

This is the complete code of the resulting program:

class Node(object):
 
    def __init__(self, data=None, next_node=None):
        self.data = data
        self.next_node = next_node
 
 
class LinkedList(object):
    def __init__(self, head=None):
        self.head = head
 
    def size(self):
     current = self.head
     count = 0
     while current:
        count += 1
        current = current.next_node
     return count
       
    def printList(self): 
        temp = self.head 
        while (temp): 
            print (temp.data, " -> ", end = '') 
            temp = temp.next_node
        print("")
 
    def insert_at_head(self, data):
      new_node = Node(data)
      new_node.next_node = self.head
      self.head = new_node
 
    def get_next_node (self,node):
      return node.next_node.data

list1 = LinkedList(Node(0))
s = list1.head
for i in range(1,6):
    s.next_node = Node(i)
    s = s.next_node
list1.printList()

list2 = LinkedList(Node(99))
w = list2.head
for j in range(98,96,-1):
    w.next_node = Node(j)
    w = w.next_node
list2.printList()

def merge(list1: LinkedList, a: int, b: int, list2: LinkedList) -> LinkedList:

    current = list1.head
    previous = None
    insertion = list2.head
    counter = 0

    while insertion.next_node:
        insertion = insertion.next_node

    while current:
        if counter == a:
            previous.next_node = list2.head
        elif counter == b:
            insertion.next_node = current.next_node
        previous = current
        counter += 1
        current = current.next_node

    return list1.printList()

print(merge(list1, 1, 3, list2))

It works as expected except only it prints an additional None in the end of an output. Why does it print None? What am I missing?

question from:https://stackoverflow.com/questions/65911797/why-do-my-program-prints-none-in-the-end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your last line prints the return of your merge call. The return of merge is list.printList(). However, list.printList() doesn't have a return defined, so it defaults to None. Note that list.printList() still prints when called; it just doesn't have a defined return.

Sounds like you just want to call merge without the printing. So the last line could be:

merge(list1, 1, 3, list2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...