All pastes #715180 Raw Edit

Stuff

public text v1 · immutable
#715180 ·published 2007-09-26 02:36 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.next == null){
			curr.next = new MyListNode(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.");
	}