[英]How to delete a given node from a linked list using Python
I am trying to learn linked list in Using python, 我正在尝试使用python学习链接列表,
Could someone please guide me how to delete a particular give node from a linked list? 有人可以指导我如何从链接列表中删除特定的给定节点吗?
#!/usr/bin/python
class Node(object):
def __init__(self, data=None, next=None):
self.data = data
self.next = next
def __str__(self):
return str(self.data)
def print_list(node):
while node:
print node,
node = node.next
print
def delete_node(node, node_to_remove):
if first_node == None:
return
pass
# way of creating linked list
def create_linked_list1(n):
linked_list = Node(1)
head = linked_list
for i in range(1, n):
head.next = Node(i)
head = head.next
return linked_list
node1 = create_linked_list1(10)
print_list(node1)
The obvious solution is this: 显而易见的解决方案是这样的:
def delete_by_index(node, index):
for _ in range(index):
prev_node, node = node, node.next
prev_node.next = node.next
However, this won't be able to delete the first node. 但是,这将无法删除第一个节点。 In order to do that, you need to make it return the new list head, which will normally be the old list head, but will instead be the old second node in the case where you deleted the head.
为此,您需要使其返回新的列表头,该列表头通常是旧的列表头,但是在删除该头的情况下,它将是旧的第二个节点。 So:
所以:
def delete_by_index(node, index):
if not index:
return node.next
head = node
for _ in range(index):
prev_node, node = node, node.next
prev_node.next = node.next
return head
It should be obvious how to simplify this, and you should do so. 如何简化这一点应该很明显,您应该这样做。
Another option is to factor out the "searching" and "deleting" parts. 另一种选择是排除“搜索”和“删除”部分。 Write an
nth
function (this should be easy), then you can do this: 编写
nth
函数(这应该很简单),然后可以执行以下操作:
def delete_by_index(node, index):
if not index:
return node.next
prev_node = nth(node, index-1)
prev_node.next = prev_node.next.next
return node
If you know about recursive functions, you might also want to figure out how to write either delete_by_index
or nth
recursively (it's one of the easiest recursive functions to write). 如果您了解递归函数,则可能还需要弄清楚如何递归地编写
delete_by_index
或nth
(这是最容易编写的递归函数之一)。
You may also want to trap the error caused by deleting, say, the 15th node of a 10-node list and make it something nicer. 您可能还想捕获由删除(例如10个节点的列表)的第15个节点引起的错误,并使其变得更好。 Figure out what error you would get in that case (or, if you can't figure it out, just run it and see), then
try
/ except
that, and raise an IndexError
instead. 弄清楚在这种情况下会遇到什么错误(或者,如果无法弄清楚,只需运行并查看),然后
try
/ except
,然后引发IndexError
。
While you're at it, you may want to add a delete_by_data(node, data)
function and maybe a delete_by_identity(node, child_node)
for further practice. 在进行此操作时,可能需要添加
delete_by_data(node, data)
函数,以及也许delete_by_identity(node, child_node)
以进行进一步练习。
Assume the following singly linked list with a pointer, head
, to the first node 假定下面的单链接列表的指针
head
指向第一个节点
head ⇢ n 0 → n 1 → … → n i - 1 → n i → n i + 1 → … → n N-1 → None
def delete(i):
if i == 0: # there is no prev node, increment head
head = head.next
else:
prev = get_node(i-1) # find prev node, node[i-1]
prev.next = prev.next.next # remove node i
Because this is a singly linked list get_node(i-1)
will have to start at head
and increment i-1
times to find the node. 因为这是一个单向链表
get_node(i-1)
将不得不开始在head
和增量i-1
次找到节点。
NOTE: If this was a doubly linked list, given node[i]
you can find node[i-1]
using node[i].prev
. 注意:如果这是一个双向链接列表,给定
node[i]
,则可以使用node[i].prev
找到node[i-1]
。
v-> w-> x->y> z and if we want to delete x so that new Linked List is v -> w-> y-> z and we have access to only x v-> w-> x-> y> z,如果我们要删除x,以使新的链表为v-> w-> y-> z,我们只能访问x
next_node = x.next next_node = x.next
x.next = next_data.next x.next = next_data.next
Deletion of a node using python in single linked list. 在单个链接列表中使用python删除节点。
def delete(self,data): def delete(self,data):
if self.head.data==data:
temp=self.head.next
del self.head
self.head=temp
else:
p=self.head
while p.next.data!=data:
p=p.next
temp=p.next.next
del p.next
p.next=temp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.