在链接列表python HackerRank的尾部插入一个节点

时间:2016-02-22 17:08:40

标签: python linked-list

这是一个在链接列表末尾添加节点的非常简单的程序。我不知道我在做什么错。它与hackerRank的外部输出有关,或者我的代码中有错误。 我正在尝试实现Python2

class Node(object):

   def __init__(self, data=None, next_node=None):
       self.data = data
       self.next = next_node
def Insert(head, data):

    if (head.head == None):
        head.head = Node(data)
    else:
        current = head.head
        while (current.next != None) and (current.data == data):
                           current = current.next
        current.next = Node(data)

这是问题的链接。 https://www.hackerrank.com/challenges/insert-a-node-at-the-tail-of-a-linked-list

7 个答案:

答案 0 :(得分:4)

如果你必须在链接列表的末尾添加,那么就不需要测试current.data == data,下面的代码就足够了 -

def Insert(head, data):

    if (head == None):
        head = Node(data)
    else:
        current = head
        while (current.next != None):
            current = current.next
        current.next = Node(data)
    return head

另请注意,在if和while之后,您不需要使用()

答案 1 :(得分:2)

您的代码有几个问题:

  • headNoneNode的实例。也没有 head属性,因此head.head毫无意义。
  • None是一个单身人士,因此请测试something is None而不是something == Nonesomething is not None而不是something != None
  • 你应该返回修改后的列表的头部。你的功能不会返回任何内容。

答案 2 :(得分:2)

这将通过适用于python3的HackerRank解释器运行

#print ('This is start of the function')
node = SinglyLinkedListNode(data)

if (head == None):
    head = node
else:
    current = head
    while (current.next != None):
        current = current.next
    current.next = node
return head

答案 3 :(得分:1)

这是答案-

#!/bin/python3

import math
import os
import random
import re
import sys

class SinglyLinkedListNode:
    def __init__(self, node_data):
        self.data = node_data
        self.next = None

class SinglyLinkedList:
    def __init__(self):
        self.head = None

def print_singly_linked_list(node, sep, fptr):
    while node:
        fptr.write(str(node.data))

        node = node.next

        if node:
            fptr.write(sep)

def insertNodeAtTail(head, data): 
    item = SinglyLinkedListNode(data)
     
    if head is None:
        head = item 
    else:
        n = head
        
        while(n.next):
            n = n.next 
            
        n.next = item
         
    return head

答案 4 :(得分:0)

节点类:

类Node(对象):     def init (self,item):         self.data = item         self.next =无

def getdata(self):
    return self.data

def setdata(self, item):
    self.data = item

def getnext(self):
    return self.next

def setnext(self, item):
    self.next = item

以及最后插入的方法是

 def insert(head, item):
    newNode = Node(item)
    newNode.setdata(item)
    current = head
    while current.getnext() != None:
        current = current.getnext()
    current.setnext(newNode)

答案 5 :(得分:0)

单独的递归版本:

def getTail(node):
    if node.next == None:
        return node
    return getTail(node.next)

def Insert(head, data):
    if head == None:
        return Node(data)
    tail = getTail(head)
    tail.next = Node(data)
    return head

答案 6 :(得分:0)

class SinglyLinkedListNode:
    def __init__(self, node_data):
        self.data = node_data
        self.next = None

class SinglyLinkedList:
    def __init__(self):
        self.head = None

# Implementation
def insertNodeAtTail(head, data):
    if head == None:
        head = SinglyLinkedListNode(data)
    else:
        current = head
        while current.next != None:
            current = current.next
        current.next = SinglyLinkedListNode(data)
    return head