Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SE102 - Linked List 1

Caren
March 27, 2019

SE102 - Linked List 1

Caren

March 27, 2019
Tweet

More Decks by Caren

Other Decks in Education

Transcript

  1. HackerRanks • Go over solution guides • Use your score

    as a way to gauge whether you need to study more
  2. What is a Linked List? Individual elements (nodes) that each

    hold two pieces of information: 1) data (integer, string, anything!) 2) reference to the next node
  3. class LinkedListNode {
 String data;
 LinkedListNode next;
 
 LinkedListNode(String data)

    {
 this.data = data;
 }
 } LinkedListNode head = LinkedListNode head = new LinkedListNode("a");

  4. class LinkedListNode {
 String data;
 LinkedListNode next;
 
 LinkedListNode(String data)

    {
 this.data = data;
 }
 } LinkedListNode head = LinkedListNode head = new LinkedListNode("a");
 LinkedListNode n1 = new LinkedListNode("b");
 LinkedListNode n2 = new LinkedListNode("c");
 LinkedListNode n3 = new LinkedListNode("d");
 

  5. class LinkedListNode {
 String data;
 LinkedListNode next;
 
 LinkedListNode(String data)

    {
 this.data = data;
 }
 } LinkedListNode head = LinkedListNode head = new LinkedListNode("a");
 LinkedListNode n1 = new LinkedListNode("b");
 LinkedListNode n2 = new LinkedListNode("c");
 LinkedListNode n3 = new LinkedListNode("d");
 
 head.next = n1;
 n1.next = n2;
 n2.next = n3;
  6. Why are they useful? Web browser’s history (previous / next

    page) Memory management (blocks of memories)
  7. Why are they useful? Web browser’s history (previous / next

    page) Memory management (blocks of memories) Hash tables (resolving collisions)
  8. Pros and Cons Always think about space / time complexity

    when choosing a data structure! Good for:
 - insertion / deletion from one end 
 

  9. Pros and Cons Always think about space / time complexity

    when choosing a data structure! Good for:
 - insertion / deletion from one end 
 
 Bad for:
 - searching
  10. Pros and Cons Always think about space / time complexity

    when choosing a data structure! Good for:
 - insertion / deletion from one end 
 
 Bad for:
 - searching https://guides.codepath.org/compsci/Linked-Lists
  11. Understand If you don’t understand the problem completely, you can’t

    solve it correctly Interviewers often give vague questions to test your ability to gather requirements
  12. Understand Asking clarifying questions
 
 - is x input possible?


    - will the input always be sorted?
 - are there any space and time constraints?

  13. Understand Asking clarifying questions
 
 - is x input possible?


    - will the input always be sorted?
 - are there any space and time constraints?
 - given x input, is the expected output y?
  14. Group Exercise Work together in groups to practice Understanding the

    problem - write out the method stub for the problem (what should this method return? what kind of inputs will should this method take in?)
 - list out at least 3 different input / output examples
 short input, long input, edge case
 - additional possible edge cases?
 - are any other clarifying questions needed?
  15. Group Exercise Work together in groups to practice Understanding the

    problem - write out the method stub for the problem (what should this method return? what kind of inputs will should this method take in?)
 - list out at least 3 different input / output examples
 short input, long input, edge case
 - additional possible edge cases?
 - are any other clarifying questions needed? The goal is to gather enough information to be able to solve the problem, but you don’t need to know how you’re going to solve it yet
  16. Example 1 Reverse a singly linked list
 
 - should

    our method return a new node?
 

  17. Example 1 Reverse a singly linked list
 
 - should

    our method return a new node?
 
 void reverse(ListNode node) {
 }
  18. Example 1 Reverse a singly linked list Given.. 1
 Do

    we return … 1 ?
 
 Given.. 1->2->3
 Do we return … 3->2->1 ?
 

  19. Example 1 Reverse a singly linked list Given.. 1
 Do

    we return … 1 ?
 
 Given.. 1->2->3
 Do we return … 3->2->1 ?
 
 Given.. 1->1->1
 Do we return… 1->1->1 ?
 

  20. Example 1 Reverse a singly linked list Given.. 1
 Do

    we return … 1 ?
 
 Given.. 1->2->3
 Do we return … 3->2->1 ?
 
 Given.. 1->1->1
 Do we return… 1->1->1 ?
 
 Can we use extra space?

  21. Example 1 Reverse a singly linked list Given.. 1
 Do

    we return … 1 ?
 
 Given.. 1->2->3
 Do we return … 3->2->1 ?
 
 Given.. 1->1->1
 Do we return… 1->1->1 ?
 
 Can we use extra space?
 Do we have to do it in one pass?
  22. Example 2 Remove duplicates from a linked list
 
 -

    do we want to return a new list or delete duplicates from the original?
 

  23. Example 2 Remove duplicates from a linked list
 
 -

    do we want to return a new list or delete duplicates from the original?
 
 void removeDuplicates(ListNode node) {
 }
  24. Example 2 Remove duplicates from a linked list What kind

    of data does the linked list hold (integers, strings)?

  25. Example 2 Remove duplicates from a linked list What kind

    of data does the linked list hold (integers, strings)?
 Do we remove the duplicates and keep one of them? ie: 1->1->2 becomes 1->2 

  26. Example 2 Remove duplicates from a linked list What kind

    of data does the linked list hold (integers, strings)?
 Do we remove the duplicates and keep one of them? ie: 1->1->2 becomes 1->2 
 Is the linked list always sorted already?

  27. Example 2 Remove duplicates from a linked list What kind

    of data does the linked list hold (integers, strings)?
 Do we remove the duplicates and keep one of them? ie: 1->1->2 becomes 1->2 
 Is the linked list always sorted already?
 Can we use extra space?

  28. Example 2 Remove duplicates from a linked list What kind

    of data does the linked list hold (integers, strings)?
 Do we remove the duplicates and keep one of them? ie: 1->1->2 becomes 1->2 
 Is the linked list always sorted already?
 Can we use extra space?
 Do we have to do it in one pass?

  29. Example 2 Remove duplicates from a linked list What kind

    of data does the linked list hold (integers, strings)?
 Do we remove the duplicates and keep one of them? ie: 1->1->2 becomes 1->2 
 Is the linked list always sorted already?
 Can we use extra space?
 Do we have to do it in one pass?
 Given… 1, Return.. 1?
 Given… 1->1->1, Return… 1?
 Given… 1->2->3->4->4->5->5, Return… 1->2->3->4->5?
  30. Group Exercise In a moment, we’re going to be assigned

    to break out rooms of 3-4 people Once you’re in your break out room, navigate to the course portal and go to Week 3’s Session 1 tab
  31. Group Exercise In a moment, we’re going to be assigned

    to break out rooms of 3-4 people Once you’re in your break out room, navigate to the course portal and go to Week 2’s Session 1 tab We’ll bring everybody back to the main room in 30 minutes (with a 5 minute warning beforehand)