这个问题是一个黑客挑战。链接到这里:https://www.hackerrank.com/challenges/insert-a-node-at-a-specific-position-in-a-linked-list
"""
Insert Node at a specific position in a linked list
head input could be None as well for empty list
Node is defined as
class Node(object):
def __init__(self, data=None, next_node=None):
self.data = data
self.next = next_node
return back the head of the linked list in the below method.
"""
#This is a "method-only" submission.
#You only need to complete this method.
def InsertNth(head, data, position):
node = head
if position == 0:
node = Node(data)
node.data = data
node.next = head
return node
else:
while position > 0:
node = node.next
i = node.next
node.next = Node(data)
node.next.next = i
return head
我当前的输出是321024,但我需要它是310542.非常感谢任何帮助!
答案 0 :(得分:3)
def InsertNth(head, data, position):
start = head
if position == 0:
return Node(data, head)
while position > 1:
head = head.next
position -= 1
head.next = Node(data, head.next)
return start
我没有测试它,但这样的事情应该是正确的。您首先要将引用的副本保存到列表的开头。然后,您应该检查是否将其添加到列表的前面,在这种情况下,您应该返回具有适当值的新起始节点。否则,您循环浏览列表,直到到达所需的位置,此时您将下一个节点设置为具有正确的值和剩余列表。
答案 1 :(得分:1)
我们有两种可能的选择。
def insertNodeAtPosition(self, head, data, position):
if (position == 0) | (head==None):
return SinglyLinkedListNode(data, head)
else:
head.next = self.insertNodeAtPosition(head.next, data, position-1)
return head
答案 2 :(得分:0)
def InsertNth(head, data, position):
node = head
i = 0
temp = Node(data)
while node is not None:
if i == at:
temp.nextnode = node.nextnode
node.nextnode = temp
break
i += 1
node = node.nextnode
答案 3 :(得分:0)
def insert(self, data, position):
counter = 1
current = self.head
if position > 1:
while current and counter < position:
if counter == position - 1:
data.next = current.next
current.next = data
current = current.next
counter += 1
elif position == 1:
data.next = self.head
self.head = new_element
答案 4 :(得分:0)
def insertNodeAtPosition(head, data, position):
prev_node = head
if position == 0:
new_node = SinglyLinkedListNode(data)
new_node.next = head
return new_node
while prev_node is not None:
new_node = SinglyLinkedListNode(data)
for _ in range(position-1):
prev_node = prev_node.next
new_node.next = prev_node.next
prev_node.next = new_node
return head
答案 5 :(得分:0)
class SinglyLinkedListNode:
def __init__(self, node_data):
self.data = node_data
self.next = None
class SinglyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def insertNodeAtPosition(head, data, position):
pos = 0
current_head = head
previous_head = head
while pos <= position:
if pos == position:
new_head = SinglyLinkedListNode(data)
previous_head.next = new_head
new_head.next = current_head
return head
else:
pos += 1
previous_head = current_head
current_head = current_head.next
return head
答案 6 :(得分:0)
def insertNodeAtPosition(head, data, position):
currNode = head
while position > 1:
position -= 1
currNode = currNode.next
temp = currNode
node = SinglyLinkedListNode(data)
node.next = temp.next
currNode.next = node
return head
答案 7 :(得分:0)
def insertNodeAtPosition(head, data, position):
positer = 0
temp = head
while temp:
if positer == position-1:
newNode = SinglyLinkedListNode(data)
newNode.next = temp.next
temp.next = newNode
break
else:
positer+=1
temp = temp.next
return head
答案 8 :(得分:0)
def insert_after(self, data, pos):
new_node = Node(data)
prev = self.head
pos = pos - 1
if pos == 0:
self.insert_front(data)
return
while pos > 1:
prev = prev.next
pos -= 1
new_node.next = prev.next
prev.next = new_node