All pastes #715182 Raw Edit

Stuff

public text v1 · immutable
#715182 ·published 2007-09-26 02:46 UTC
rendered paste body
public void add(int index, T element) {
	MyListNode prev = head;
	MyListNode curr = head;
	MyListNode oldNode;
	MyListNode newNode= new MyListNode(element);
	int i = 0;
	if(index<0 || index>this.size()){
		throw new IndexOutOfBoundsException("Index out of bounds!");
	}		
	if(curr.val == null){
		curr.val= element;
	}else{
		while(curr!=null ){
			if(index == 0){
				oldNode = new MyListNode(head.val);
				oldNode.next = head.next;					head = newNode;
					head.next = oldNode;
					i++;
				}else
				if(i == index && index != 0){
					oldNode = new MyListNode(curr.next.val);
					
					oldNode.next = newNode;
					newNode.next = curr;
					//newNode.next = oldNode;
					i++;
					//curr = oldNode;
				}else{
					prev = curr;
					curr = curr.next;
					i++;
					
				}

				
			}
		}
		//throw new UnsupportedOperationException("You must implement this method.");
	}