简体   繁体   English

在对象作为单链接列表中的参数传递之前删除元素

[英]Removing an element before the object passed in as the parameter in a Singly Linked List

I'm trying to write a method for to remove an element before the object passed in as a parameter in a Singly Linked List. 我正在尝试编写一种方法,以在对象作为单链接列表中的参数传入之前删除元素。

So far I have this, but honestly I'm completely lost. 到目前为止,我已经拥有了,但是老实说我完全迷失了。

public void removeElementBefore(Object o) {

  Node n = new Node(o);
  Node crt = head;

  // trying to find the element right before the parameter
  while(crt.next != n)
    crt = crt.next;

  // getting the new content
  n = crt.next.content;

  return n;
  }
}

More of the program for context: method to remove last element in the list 更多用于context的程序:删除列表中最后一个元素的方法

public Object removeLast() throws EmptyListException {

    if (head == null) throw new EmptyListException();

    Object o;

    // If there is only one element, we need to modify the head of the list
    if (head.next == null) {
      o = head.content;
      head.content = null;
      head = null;
      return o;
    }

    // We need to go to the second-to-last element
    Node crt = head;
    while (crt.next.next != null)
      crt = crt.next;

    // Now get the content
    o = crt.next.content;

    // Remove all references that are not needed
    crt.next.content = null;
    crt.next = null;

    // And we're done
    return o;
  }
public Object removeElementBefore(Object o) {
    if (o == null)
        return null;

    Node prev = head;
    Node crt = prev == null ? null : prev.next;
    if (crt == null)
        return null;

    // trying to find the element right before the parameter
    while(crt.next != null && !o.equals(crt.next.content)) {
        prev = prev.next;
        crt = crt.next;
    }

    if (crt.next == null)
        return null;

    // remove crt node from list
    prev.next = crt.next;
    crt.next = null;

    // return the removed content
    return crt.content;
}

Here's a non-Java specific algorithm (not necessarily completely compact). 这是非Java专用算法(不一定完全紧凑)。 You should be able to translate it into Java code. 您应该能够将其转换为Java代码。

Given linked list L and item to remove item . 给定链接列表L和要删除项目的项目

cur = L.head
if (cur == item)
   L.head = cur.next
   return
prev = cur
cur = cur.next
while (cur != item):
    if NOT (cur.next exists):
        // cur is the end of the list, so the item doesn't exist in this list
        return
    prev = cur
    cur = cur.next

// If you reach this point, the desired item has been found, and prev
// precedes it in the list
prev.next = cur.next
public void removeElementBefore(Node before) {
    if (before == null) return;
    Node n = head;

    if (n.next == before) {
        head = n.next;
        return;
    }

    while (n.next.next != before) { 
        if (n.next.next == null) return;
        n = n.next;
    }
    n.next = n.next.next;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM