What is LinkedListNode in Java

Sorry my ignorance, but I'm starting to prepare for my first technical interview and stumbled upon this question and answer on a related list topic

Question: Implement an algorithm to remove a node in the middle of one linked list, considering only access to this node

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

I want to start playing with this code (make changes to the compilation), but I'm not sure how to start doing this in Java. I cannot find the LinkedListNode class in Java docs.

This may be a very stupid question, but if someone can point me in the right direction, appreciate it.

EDIT

Thanks for the quick and helpful answers. I think my question was not very clear. The algorithm above was provided as a solution to this issue. I wanted to know how to implement this in Java so that I can play with the code.

thanks

+8
java linked-list
source share
5 answers

The code will only work correctly if there is a node tail in 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 a tail guard is so we can make "next" be the "tail" node of the list. (Its data is null and it next is null.) Otherwise, "prev".next would still be pointing to something. 

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

 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(); } } 
+13
source share

LinkedListNode is the class you define for storing data. To make your above example work, I quickly wrote this code (just to make you understand a simple concept) in which I create 3 nodes (which are connected to each other) and then delete the middle of the deleteNode method call that you indicated in your question.

The code is pretty clear. Let me know if this helps. Good luck

 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; } } 
+6
source share

This class is most likely the hypothetical class used for this linked list .

+5
source share

Your question is a bit confusing. Do you want the logic to delete the node in the list of the same name, or do you want to learn and use java LinkedlistNode.

if you are in the second link, it will help you

LinkedListNodee

or if you want logic

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

Important details of this question relate to data structures, java is just the language that is used for implementation in this case.
You should read the wikipedia article on linked lists , and be careful for this question that your solution doesn’t cause any freezing links or orphan sites .
Do a couple of searches in two terms in bold and make sure you understand them.

0
source share

All Articles