Can anybody help me in optimizing this code?
class Node{
public int value;
public Node next;
Node(int x){
value = x;
next = null;
}
}
public class MyLinkedList {
private Node head;
/** Initialize your data structure here. */
// public MyLinkedList() {
// head = new Node();
// }
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
int i=0;
Node n = head;
while(n.next!=null){
if(i==index) break;
n = n.next;
i++;
}
if(i==index){
System.out.println(n.value);
return n.value;
}else{
return -1;
}
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
public void addAtHead(int val) {
Node n = new Node(val);
if (head==null) {
this.head = n;
} else {
n.next = this.head;
head = n;
}
System.out.println(n.value);
// System.out.println(n.next.value);
}
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
if(this.head == null) {
head = new Node(val);
return;
}
Node n = this.head;
while(n.next!=null){
n = n.next;
}
n.next = new Node(val);
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
public void addAtIndex(int index, int val) {
Node n = this.head;
Node newNode = new Node(val);
int i = 0;
int length =0;
if(index == 0) {
newNode.next = this.head;
this.head = newNode;
return;
}
if(this.head==null) length =0;
else length =1;
while(n.next!=null){
length++;
if(i+1==index){
break;
}
i++;
n = n.next;
}
if(length==index){
n.next = newNode;
// newNode.next = null;
}
else if(i+1 == index){
// System.out.println(i +"");
newNode.next = n.next;
n.next = newNode;
}
// System.out.println(n.value);
// System.out.println(n.next.value);
// System.out.println(n.next.next.value);
//
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
Node n = this.head;
int length = 1;
Node tempNode = this.head;
while (tempNode.next!=null) {
tempNode = tempNode.next;
length++;
}
if (index>length) {
return;
}
if (index == 0) {
this.head = this.head.next;
return;
}
int i = 0;
while(n.next != null){
if(i+1==index) break;
i++;
n = n.next;
}
if (index==length) {
n.next = null;
}
else if(i+1==index){
n.next = n.next.next;
}
}
public void output(){
Node n = this.head;
while(n.next!= null){
System.out.print(n.value);
n = n.next;
}
System.out.print(n.value);
}
public static void main(String[] args) {
// return 3
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(2);
myLinkedList.output();
myLinkedList.deleteAtIndex(1);
myLinkedList.output();
// now the linked list is 1->3
myLinkedList.addAtHead(2);
myLinkedList.output();
myLinkedList.addAtHead(7);
myLinkedList.output();
myLinkedList.addAtHead(3);
myLinkedList.output();
myLinkedList.addAtHead(2);
myLinkedList.output();
// myLinkedList.addAtTail(3);
// myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
// myLinkedList.get(1); // return 2
// myLinkedList.deleteAtIndex(1); // now the linked list is 1->3
// myLinkedList.get(1); // return 3
}
}