JAVA 问题 高手进入 帮帮忙, 谢谢了

来源:百度知道 编辑:UC知道 时间:2024/05/19 22:40:47
a) the following code gives ther partially completed implementation of a Singly Linked List. In this implementation, write the addToHead() method. Note that the instance variables of the Node class are defined as public so you can manipulate them directly.

public class node {
public Object data;
public Node next;
}

public class SinglyLinkedList {
private Node head;
private int size;

public SinglyLinkedList() {
head = null;
size = 0;
}

public void addToHead(Object o){
//complete this method

b)Write the addToTail()method for the SinglyLinkedList class.

public void addToTail(Object o){
//complete this method

c)What is the big-O running time of the addToHead() method?

d)What is the big-O running time of the addToTail() method?

c) 是不是log(n)
d) 是不是n^2

addToHead:
Node node=new Node();
node.data=o;
if(size==0)
head=node;
else{
Node temp=head;
head=node;
node.next=temp;
}

addToTail:
Node node=new Node();
node.data=o;
if(size==0)
head=node;
else{
Node temp=head;
while(temp.next!=null)
temp=temp.next;
temp.next=node;
}
算法复杂度为O(1)和O(n)啊。。

我觉得是这样,是不是我没看明白题啊

我和楼上的理解一样。