Skip to content

Latest commit

 

History

History
49 lines (42 loc) · 859 Bytes

exercise-answers.md

File metadata and controls

49 lines (42 loc) · 859 Bytes

CSE 143

Exercise Answers

Week 2

  1. One possible answer is shown below.

    public void shift(Stack<Integersint n) {
    	Queue<Integerq = new LinkedList<Integer>();
    	int size = s.size();
    	    
    	for (int i = 0i < size - ni++) {
    		q.add(s.pop());
    	}
    	    
    	while (!q.isEmpty()) {
    		s.push(q.remove());
    	}
    	    
    	while (!s.isEmpty()) {
    		q.add(s.pop());
    	}
    
    	while (!q.isEmpty()) {
    		s.push(q.remove());
    	}
    }
  2. One possible answer is shown below.

    ListNode q = p;
    p = p.next;
    q.next = null;
  3. One possible answer is shown below.

    ListNode list2 = list.next.next;
    list.next.next = null;
    list2.next.next = list.next;
    list.next = null;
    list2.next.next.next = list;
    list = list2.next;
    list2.next = list.next.next;
    list.next.next = null;