[英]How to remove last node from linked list without using double pointer
Althought it is much easier to solve the problem with double pointers, is it possible to remove the last node from a singly linked list without using double pointer?虽然用双指针解决问题要容易得多,但是否可以在不使用双指针的情况下从单链表中删除最后一个节点? I am working on an easy question 203. Remove Linked List Elements , following is my answer, but it failed to remove the last node.
我正在处理一个简单的问题203. Remove Linked List Elements ,以下是我的答案,但它未能删除最后一个节点。 I do not understand why it failed.
我不明白为什么它失败了。 COuld you please explain why?
你能解释一下为什么吗? But a similar method used here seems to work well.
但是这里使用的类似方法似乎效果很好。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
if not head:
return head
res = head
while head:
if head.val == val and head.next:
head.val = head.next.val
head.next= head.next.next
elif head.val == val and not head.next:
head = None
else:
head = head.next
return res
input:
[1,2,6,3,4,5,6]
6
output:
[1,2,3,4,5,6]
Try to use a dummy head.尝试使用虚拟头。
Linked-list looks like this: [Dummy_head->1->2->3->4->5->6]
6.next = None链表如下所示:
[Dummy_head->1->2->3->4->5->6]
6.next = None
We are going to find a node which next
points to the node which is going to be removed.我们将找到一个节点,该节点
next
指向将要删除的节点。
prev = dummy_head
for i in range(index): # this 'index' should be the index of the last element.
prev = dummy_head.next
ret_node = prev.next
prev.next = ret_node.next
ret_node.next_node = None # do something here to remove the element.
I tried to solve Leetcode question 203.我试图解决 Leetcode 问题 203。
I solved it with java.我用 java 解决了它。
public ListNode removeElement(ListNode head, int val)
{
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
LinkedNode prev = dummyHead;
while(prev.next != null)
{
if(prev.next.val == val)
prev.next = prev.next.next;
else:
prev = prev.next;
}
return dummyHead.next; // "DummyHead" process shields the user.
}
You can think of this as a pseudo-code.您可以将其视为伪代码。
You need to handle leaf node specially.您需要专门处理叶节点。 the below code works:
以下代码有效:
def remove_element(head, val):
if None == head:
return None
start = head
while val == start.val:
start = start.next
cur = start
while None != cur.next:
if val == cur.next.val:
if None != cur.next.next:
cur.next = cur.next.next
else:
cur.next = None
break
cur = cur.next
return head
Just like the other answer says, it'd be best to use a sentinel node:就像其他答案所说,最好使用哨兵节点:
class Solution:
def removeElements(self, head, val):
dummy = ListNode(-1)
dummy.next, curr = head, dummy
while curr.next:
if curr.next.val == val:
curr.next = curr.next.next
else:
curr = curr.next
return dummy.next
Here is also a very similar LeetCode's solution:这里也是一个非常类似 LeetCode 的解决方案:
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
sentinel = ListNode(0)
sentinel.next = head
prev, curr = sentinel, head
while curr:
if curr.val == val:
prev.next = curr.next
else:
prev = curr
curr = curr.next
return sentinel.next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.