Skip to content

Latest commit

 

History

History
52 lines (45 loc) · 1.15 KB

check-in-meeting-answers.md

File metadata and controls

52 lines (45 loc) · 1.15 KB

CSE 143

Check-in Meeting Answers

Week 2

  1. One possible answer is shown below.

    public boolean isConsecutive(Stack<Integers) {
        if (s.size() < 2) {
            return false;
        }
        
        Queue<Integerq = new LinkedList<Integer>();
        boolean isConsecutive = true;
        
        int curr = s.pop();
        while (!s.isEmpty()) {
            int next = s.pop();
            isConsecutive &= curr - 1 == next;
            q.add(curr);
            curr = next;
        }
        
        q.add(curr);
        
        while (!q.isEmpty()) {
            s.push(q.remove());
        }
        
        while (!s.isEmpty()) {
            q.add(s.pop());
        }
        
        while (!q.isEmpty()) {
            s.push(q.remove());
        }
        
        return isConsecutive;
    }
  2. One possible answer is shown below.

    ListNode temp = q.next;
    q.next = q.next.next;
    q.next.next = p.next.next;
    temp.next = p.next;
    temp.next.next = p;
    temp.next.next.next = null;
    p = temp;