Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * addAtIndex( ):
- index < 0 : do nothing.
- index > size: add to end
- *
- */
- public void addAtIndex(int index, E data) {
- if (index < 0)
- return;
- if (index > size())
- add(data);
- if (data != null)
- head = addAtIndex(head, index, data);
- }
- private LLNode<E> addAtIndex(LLNode<E> t, int index, E data) {
- LLNode<E> n;
- if (index == 0) {
- n = new LLNode<E>(data); // make new node
- n.setNext(t); // point to current node
- t = n; // check
- }
- else {
- t.setNext(addAtIndex(t.getNext(), index-1, data));
- }
- return t;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement