Skip to content

Latest commit

 

History

History
77 lines (70 loc) · 1.59 KB

check-in-meeting-answers.md

File metadata and controls

77 lines (70 loc) · 1.59 KB

CSE 143

Check-in Meeting Answers

Week 9

  1. hasDuplicates

    public boolean hasDuplicates() {
    	ListNode current = front;
    	while (current != null) {
    		ListNode comparison = current.next;
    		while (comparison != null) {
    			if (comparison.data == current.data) {
    				return true;
    			}
    			else {
    				comparison = comparison.next;
    			}
    		}
    		current = current.next;
    	}
    	return false;
    }
  2. addListAt

    public void addListAt(ListNode list, int index) {
    	if (index < 0) {
    		throw newIllegalArgumentException("index negative");
    	}
    	if (list != null) {
    		ListNode oldListAfterIndex = front;
    		ListNode oldListBeforeIndex = null;
    		for(int i = 0; i < index; i++) {
    			if (oldListAfterIndex == null) {
    				throw new IllegalArgumentException("index beyond list");
    			}
    			oldListBeforeIndex = oldListAfterIndex;
    			oldListAfterIndex = oldListAfterIndex.next;
    		}
    		ListNode newListLast = list;
    		while (newListLast.next != null) {
    			newListLast = newListLast.next;
    		}
    		if (oldListBeforeIndex == null) {
    			front = list;
    		}
    		else {
    			oldListBeforeIndex.next = list;
    		}
    		newListLast.next = oldListAfterIndex;
    	}
    }
  3. countPathsOfLength

    public int countPathsOfLength(int length) {
    	return countPathsOfLength(this.root, length);
    }
    
    private int countPathsOfLength(IntTreeNode current, int length) {
    	if (current == null && length != 0) {
    		return 0;
    	}
    	else if (current == null && length == 0) {
    		return 1;
    	}
    	else {
    		return countPathsOfLength(current.left, length1) + 
    				countPathsOfLength(current.right, length1);
    	}
    }