linkedlistnode java,什么是Java中的LinkedListNode

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: Implement an algorithm to delete a node in the middle of a single linked list, given only access to that 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 (making changes compile test) but I'm not sure how to start doing this in Java. I cannot find the LinkedListNode class in Java docs.

This might be a very silly question but if someone can point me in the right direction - will appreciate it.

EDIT

Thanks for the quick and useful responses. I guess my question was not very clear. The algorithm above was provided as a solution to that question. I wanted to know how to implement that in Java so I can play around with the code.

thanks

解决方案

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java 代码示例,用于可视化设计环形链表的结构: ```java import javax.swing.*; import java.awt.*; public class CircularLinkedListVisualization extends JFrame { private LinkedListNode startNode; private int nodeCount; public CircularLinkedListVisualization(LinkedListNode startNode, int nodeCount) { this.startNode = startNode; this.nodeCount = nodeCount; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(600, 400); setLocationRelativeTo(null); setVisible(true); } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int centerX = getWidth() / 2; int centerY = getHeight() / 2; int radius = Math.min(centerX, centerY) - 50; double angleStep = 2 * Math.PI / nodeCount; LinkedListNode currentNode = startNode; for (int i = 0; i < nodeCount; i++) { int x = (int) (centerX + radius * Math.cos(i * angleStep)); int y = (int) (centerY + radius * Math.sin(i * angleStep)); g2d.setColor(Color.LIGHT_GRAY); g2d.fillOval(x - 20, y - 20, 40, 40); g2d.setColor(Color.BLACK); g2d.drawOval(x - 20, y - 20, 40, 40); g2d.drawString(String.valueOf(currentNode.getValue()), x, y); currentNode = currentNode.getNext(); } g2d.setColor(Color.BLACK); g2d.drawOval(centerX - radius - 20, centerY - radius - 20, (radius + 20) * 2, (radius + 20) * 2); g2d.drawString("Start", centerX - radius - 20, centerY - radius - 40); g2d.drawString("End", centerX + radius + 20, centerY + radius + 40); g2d.drawLine(centerX - radius - 20, centerY, centerX - radius, centerY); g2d.drawLine(centerX + radius + 20, centerY, centerX + radius, centerY); } public static void main(String[] args) { LinkedListNode startNode = new LinkedListNode(1); LinkedListNode node2 = new LinkedListNode(2); LinkedListNode node3 = new LinkedListNode(3); LinkedListNode node4 = new LinkedListNode(4); startNode.setNext(node2); node2.setNext(node3); node3.setNext(node4); node4.setNext(startNode); CircularLinkedListVisualization visualization = new CircularLinkedListVisualization(startNode, 4); } } class LinkedListNode { private int value; private LinkedListNode next; public LinkedListNode(int value) { this.value = value; } public int getValue() { return value; } public LinkedListNode getNext() { return next; } public void setNext(LinkedListNode next) { this.next = next; } } ``` 这个示例代码创建了一个环形链表,其的四个节点形成了一个环形链表。节点使用圆形的形状,箭头表示节点之间的关系。通过 `JFrame` 类和 `Graphics` 类将图形显示出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值