【问题标题】:What is LinkedListNode in JavaJava中的LinkedListNode是什么
【发布时间】:2011-07-19 10:43:21
【问题描述】:

请原谅我的无知,但我开始准备我的第一次技术面试,并在主题链表上遇到了这个问题和答案

问题:实现一个算法来删除单个链表中间的一个节点,只允许访问该节点

公共静态布尔删除节点(LinkedListNode n){ if (n == null || n.next == null) { 返回假; // 失败 } LinkedListNode next = n.next; n.data = next.data; n.next = 下一个.next; 返回真; }

我想开始使用此代码(更改编译测试),但我不确定如何在 Java 中开始执行此操作。我在 Java 文档中找不到 LinkedListNode 类。

这可能是一个非常愚蠢的问题,但如果有人能指出我正确的方向 - 将不胜感激。

编辑

感谢您快速而有用的回复。我想我的问题不是很清楚。提供上述算法作为该问题的解决方案。我想知道如何在 Java 中实现它,这样我就可以玩弄代码了。

谢谢

【问题讨论】:

    标签: java linked-list


    【解决方案1】:

    此类很可能是用于此Linked List 示例问题的假设类。

    【讨论】:

    • 是的,我就是这么想的。那么有没有简单的例子在java中实现这个算法
    【解决方案2】:

    你的问题有点混乱。无论您是想要一个逻辑来删除单链表中的一个节点,还是您想学习和使用 java LinkedlistNode。

    如果你是第二个,下面的链接会帮助你

    LinkedListNodee

    或者如果你想要逻辑

    let P the pointer to the current node
    P->data = P->next->data
    Q=P->next
    P->next=Q->next
    delete(Q)
    

    【讨论】:

      【解决方案3】:

      这个问题中的重要细节与数据结构有关,java 只是在这种情况下用于实现的语言。
      您应该阅读关于 linked lists 的维基百科文章,对于这个问题,请注意您的解决方案不会产生任何 悬空引用孤立节点
      用粗体字搜索这两个词,并确保你理解它们

      【讨论】:

        【解决方案4】:

        只有在链表上有尾节点时,代码才能正常工作。

        算法的工作逻辑如下

        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();
            }
        
        }
        

        【讨论】:

        • 非常感谢您清楚地解释这是如何工作的。所以我想知道如何在 java 中实现它。
        • @Riamo 你是什么意思?您已经列出了实现此功能的代码。
        • @glowcoder - 谢谢 - 代码不起作用 - 如 LinkedListNode 不是 java sdk 中的类
        • 是的,所以你可以像这样添加自己的类
        • @riamo 我会对你说实话。我不是说这是刻薄的,我只是希望你做得好。对于一个 4 年的计算机科学学位,人们预计在大约 2 年内能够做到这一点。所以你可能有一些事情要做。这确实是很好。只需要以正确的态度前进。尽你所能学习并在编写代码之前专注于解决问题。 (一旦问题解决,代码如下。)如果你想要一个解决问题的好例子,就拿我刚刚写的 LinkedList 和 LinkedListNode 做双重链接。 :-)
        【解决方案5】:

        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;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-12
          • 2016-09-02
          • 2010-12-10
          • 1970-01-01
          相关资源
          最近更新 更多