What is LinkedListNode in Java

前端 未结 5 664
伪装坚强ぢ
伪装坚强ぢ 2021-02-14 18:42

Excuse my ignorance but I am beginning to prepare for my first technical interview and came across this question and answer on the topic linkedlist

Question: Imp

5条回答
  •  滥情空心
    2021-02-14 19:06

    The code will only work properly if there's a tail node on the list.

    The algorithm works with the following logic

    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.
    

    Here's a class that uses LinkedListNode. I should note that if you're applying for a position as a programmer, you should be able to do this basically from memory. :-)

    class LinkedList {
    
        static class LinkedListNode {
            E data;
            LinkedListNode next;
        }
    
        /**
         * Not exactly the best object orientation, but we'll manage
         */
        static  E deleteNode(LinkedListNode node) {
            if(node == null || node.next == null) return null;
    
            E retval = node.data;
            LinkedListNode next = node.next;
            node.data = next.data;
            node.next = next.next;
            return retval;
        }
    
        private LinkedListNode head;
        private LinkedListNode tail;
    
        public LinkedList() {
            this.head = new LinkedListNode();
            this.tail = new LinkedListNode();
            head.next = tail;
        }
    
        public void addLast(E e) {
            LinkedListNode node = new LinkedListNode(); // e and next are null
            tail.data = e;
            tail.next = node;
            tail = node;
        }
    
        public void addFirst(E e) {
            LinkedListNode node = new LinkedListNode(); // e and next are null;
            node.next = head.next;
            node.data = e;
            head.next = node;
        }
    
        public E deleteFirst() {
            LinkedListNode 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 findFirst(E e) {
            LinkedListNode 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 curr = head.next;
            while(curr.next != null) {
                System.out.println(curr.data);
                curr = curr.next;
            }
        }
    
    
        public static void main(String[] args) {
            LinkedList list = new LinkedList();
            list.addLast("Apple");
            list.addLast("Bear");
            list.addLast("Chair");
            list.addLast("Dirt");
    
            //list.print();
    
            LinkedListNode bear = list.findFirst("Bear");
            deleteNode(bear);
    
            list.print();
        }
    
    }
    

提交回复
热议问题