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

SE102 - Linked List 1

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=47 Caren
March 27, 2019

SE102 - Linked List 1

7bf2baf0d8d5d7d96e6b67c5d567e3f9?s=128

Caren

March 27, 2019
Tweet

More Decks by Caren

Other Decks in Education

Transcript

  1. Linked Lists Week 3

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

    as a way to gauge whether you need to study more
  3. What is a Linked List?

  4. What is a Linked List? Individual elements (nodes) that each

    hold two pieces of information:
  5. 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
  6. None
  7. class LinkedListNode {
 String data;
 LinkedListNode next;
 
 LinkedListNode(String data)

    {
 this.data = data;
 }
 }
  8. class LinkedListNode {
 String data;
 LinkedListNode next;
 
 LinkedListNode(String data)

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

  9. 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");
 

  10. 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;
  11. Why are they useful?

  12. Why are they useful? Web browser’s history (previous / next

    page)
  13. Why are they useful? Web browser’s history (previous / next

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

    page) Memory management (blocks of memories) Hash tables (resolving collisions)
  15. Types of Linked Lists

  16. Singly linked Types of Linked Lists

  17. Singly linked Doubly linked Types of Linked Lists

  18. Singly linked Doubly linked Circularly linked Types of Linked Lists

  19. Singly linked Doubly linked Circularly linked Types of Linked Lists

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

    when choosing a data structure!
  21. Pros and Cons Always think about space / time complexity

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

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

    when choosing a data structure! Good for:
 - insertion / deletion from one end 
 
 Bad for:
 - searching
  23. 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
  24. Understand Match Pseudocode / Plan Implement Review Evaluate

  25. Understand Match Pseudocode / Plan Implement Review Evaluate

  26. Understand If you don’t understand the problem completely, you can’t

    solve it correctly
  27. 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
  28. Understand Asking clarifying questions

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


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


    - will the input always be sorted?

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


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

  32. 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?
  33. Group Exercise Work together in groups to practice Understanding the

    problem
  34. 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?
  35. 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
  36. Example 1 Reverse a singly linked list

  37. Example 1 Reverse a singly linked list
 
 - should

    our method return a new node?
 

  38. Example 1 Reverse a singly linked list
 
 - should

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

    we return … 1 ?
 

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

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

  41. 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 ?
 

  42. 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?

  43. 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?
  44. Example 2 Remove duplicates from a linked list
 
 -

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

  45. 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) {
 }
  46. Example 2 Remove duplicates from a linked list What kind

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

  47. 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 

  48. 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?

  49. 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?

  50. 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?

  51. 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?
  52. Group Exercise In a moment, we’re going to be assigned

    to break out rooms of 3-4 people
  53. 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
  54. 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)
  55. Let’s discuss the problems together!

  56. Questions?