请原谅我的无知,但我开始准备第一次技术面试,并在主题链接列表中遇到了这个问题和答案
问题:实现一种算法,删除单个链表中间的节点,只允许访问该节点
public static boolean deleteNode(LinkedListNode n) { if (n == null || n.next == null) { return false; // Failure } LinkedListNode next = n.next; n.data = next.data; n.next = next.next; return true; }
我想开始玩这个代码(进行更改编译测试),但我不确定如何在Java中开始这样做。我在Java文档中找不到LinkedListNode类。
这可能是一个非常愚蠢的问题,但如果有人能指出我正确的方向 - 会很感激。
修改
感谢快速有用的回复。我想我的问题不是很清楚。上面的算法是作为该问题的解决方案提供的。我想知道如何在Java中实现它,以便我可以使用代码。
感谢
答案 0 :(得分:13)
如果列表中有尾节点,代码将只能正常工作。
该算法使用以下逻辑
When referring to the node to be deleted, call it "curr"
When referring to the node before "curr", call it "prev"
When referring to the node after "curr", call it "next"
To effectively delete our node, "prev".next should point to "next"
It currently points to "curr"
Our problem is that we have no reference to "prev"
We know "prev".next points to "curr"
Since we cannot change the fact that "prev".next points to "curr",
we must have "curr" gobble up "next"
We make "curr"s data be "next"s data
We make "curr"s next be "next"s next
The reason this only works if there's a tail guard
is so we can make "next" be the "tail" node of the
list. (Its data is null and it's next is null.) Otherwise,
"prev".next would still be pointing to something.
这是一个使用LinkedListNode的类。我应该注意,如果你申请一个程序员职位,你应该能够从记忆中做到这一点。 : - )
class LinkedList<E> {
static class LinkedListNode<E> {
E data;
LinkedListNode<E> next;
}
/**
* Not exactly the best object orientation, but we'll manage
*/
static <E> E deleteNode(LinkedListNode<E> node) {
if(node == null || node.next == null) return null;
E retval = node.data;
LinkedListNode<E> next = node.next;
node.data = next.data;
node.next = next.next;
return retval;
}
private LinkedListNode<E> head;
private LinkedListNode<E> tail;
public LinkedList() {
this.head = new LinkedListNode<E>();
this.tail = new LinkedListNode<E>();
head.next = tail;
}
public void addLast(E e) {
LinkedListNode<E> node = new LinkedListNode<E>(); // e and next are null
tail.data = e;
tail.next = node;
tail = node;
}
public void addFirst(E e) {
LinkedListNode<E> node = new LinkedListNode<E>(); // e and next are null;
node.next = head.next;
node.data = e;
head.next = node;
}
public E deleteFirst() {
LinkedListNode<E> first = head.next;
head.next = first.next;
return first.data;
}
public E deleteLast() {
// cannot do without iteration of the list! :-(
throw new UnsupportedOperationException();
}
public LinkedListNode<E> findFirst(E e) {
LinkedListNode<E> curr = head.next;
while(curr != null) {
if(curr.data != null && curr.data.equals(e)) return curr;
curr = curr.next;
}
return null;
}
public void print() {
LinkedListNode<E> curr = head.next;
while(curr.next != null) {
System.out.println(curr.data);
curr = curr.next;
}
}
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.addLast("Apple");
list.addLast("Bear");
list.addLast("Chair");
list.addLast("Dirt");
//list.print();
LinkedListNode<String> bear = list.findFirst("Bear");
deleteNode(bear);
list.print();
}
}
答案 1 :(得分:6)
此类很可能是用于此Linked List示例问题的假设类。
答案 2 :(得分:6)
LinkedListNode是您将定义用于保存数据的类。为了让你的上面的例子工作 - 我已经快速编写了这段代码(只是为了让你理解这个简单的概念),我在其中创建3个节点(相互链接),然后删除调用 deleteNode 方法。
代码非常自我解释。如果这有帮助,请告诉我。 祝你好运
class LinkedListNode
{
public Object data;
public LinkedListNode next;
public LinkedListNode(Object data) {
this.data = data;
}
}
class DeleteNodeLinkedList
{
public static void main(String[] args)
{
LinkedListNode node_1 = new LinkedListNode("first");
LinkedListNode node_2 = new LinkedListNode("second");
node_1.next = node_2;
LinkedListNode node_3 = new LinkedListNode("third");
node_2.next = node_3;
System.out.println("*** Print contents of linked list");
LinkedListNode current = node_1;
while (current != null) {
System.out.println(current.data);
current = current.next;
}
System.out.println("*** Now delete second node");
deleteNode(node_2);
System.out.println("*** Print after deleting second node");
current = node_1;
while (current != null) {
System.out.println(current.data);
current = current.next;
}
}
public static boolean deleteNode(LinkedListNode n)
{
if (n == null || n.next == null) {
return false; // Failure
}
LinkedListNode next = n.next;
n.data = next.data;
n.next = next.next;
return true;
}
}
答案 3 :(得分:1)
这个问题的重要细节与数据结构有关,java就是这种情况下用来实现的语言。
您应该阅读有关linked lists的维基百科文章,对于这个问题,请注意您的解决方案不会产生任何悬空参考或孤立节点。
用粗体对这两个术语进行一些搜索,并确保你理解它们
答案 4 :(得分:0)
你的问题有点令人困惑。是否需要逻辑删除单个链表中的节点,或者您想学习和使用java LinkedlistNode。
如果您在第二位,以下链接将帮助您
或者如果你想要逻辑
let P the pointer to the current node
P->data = P->next->data
Q=P->next
P->next=Q->next
delete(Q)