Caren
March 27, 2019
830

March 27, 2019

## Transcript

4. ### Linked List Tips and Tricks Take multiple passes through the

linked list  - get length   - save other information about contents Two pointers  - ‘race car’ strategy with one regular pointer, and one fast pointer
5. ### Linked List Tips and Tricks Take multiple passes through the

linked list  - get length   - save other information about contents Two pointers  - ‘race car’ strategy with one regular pointer, and one fast pointer Dummy node  - helpful for preventing errors when returning ‘head’

9. ### Match Think about what data structures might be helpful for

the problem (hash map, queue, binary search, …)
10. ### Match Think about what data structures might be helpful for

the problem (hash map, queue, binary search, …) See if there are any speciﬁc techniques that you can apply (ie : using a dummy node, or taking multiple passes)
11. ### Plan / Pseudocode Start to ﬁgure out how you would

solve the problem
12. ### Plan / Pseudocode Start to ﬁgure out how you would

solve the problem Can you create any ‘magic’ helper methods that would simplify the solution? (ie : getLength(), reverse() )
13. ### Plan / Pseudocode Start to ﬁgure out how you would

solve the problem Can you create any ‘magic’ helper methods that would simplify the solution? (ie : getLength(), reverse() ) Talk through different approaches you can take, and their tradeoffs
14. ### Plan / Pseudocode Start to ﬁgure out how you would

solve the problem Can you create any ‘magic’ helper methods that would simplify the solution? (ie : getLength(), reverse() ) Talk through different approaches you can take, and their tradeoffs Be able to verbally describe your approach and explain how an example input would produce the desired output

16. ### Example: Remove Nth Node from End of List Understand: Always

start by understanding the question and running through an example
17. ### Example: Remove Nth Node from End of List Understand: Always

start by understanding the question and running through an example 1->2->3->4->5, n = 2
18. ### Example: Remove Nth Node from End of List Understand: Always

start by understanding the question and running through an example 1->2->3->4->5, n = 2  return 1->2->3->5
19. ### Example: Remove Nth Node from End of List Understand: Always

start by understanding the question and running through an example 1->2->3->4->5, n = 2  return 1->2->3->5 Let’s assume we start without any time / space constraints. So our goal is to ﬁgure out ANY kind of solution to produce the right answer
20. ### Example: Remove Nth Node from End of List Match :

Are there any special techniques that we can use to help make this easier?
21. ### Example: Remove Nth Node from End of List Match :

Are there any special techniques that we can use to help make this easier?  -> We can take a ﬁrst pass through the linked list to store the elements in an array
22. ### Example: Remove Nth Node from End of List Match :

Are there any special techniques that we can use to help make this easier?  -> We can take a ﬁrst pass through the linked list to store the elements in an array   -> We can also take a pass to get the length of the linked list
23. ### Example: Remove Nth Node from End of List Match :

Are there any special techniques that we can use to help make this easier?  -> We can take a ﬁrst pass through the linked list to store the elements in an array   -> We can also take a pass to get the length of the linked list Plan : What are some different ways we can solve this problem?
24. ### Example: Remove Nth Node from End of List Match :

Are there any special techniques that we can use to help make this easier?  -> We can take a ﬁrst pass through the linked list to store the elements in an array   -> We can also take a pass to get the length of the linked list Plan : What are some different ways we can solve this problem?  1) We can store the elements in an array, get the length of the array, and then rebuild our list excluding the nth to last node
25. ### Example: Remove Nth Node from End of List Match :

Are there any special techniques that we can use to help make this easier?  -> We can take a ﬁrst pass through the linked list to store the elements in an array   -> We can also take a pass to get the length of the linked list Plan : What are some different ways we can solve this problem?  1) We can store the elements in an array, get the length of the array, and then rebuild our list excluding the nth to last node  2) We can follow the ﬁrst idea, but instead of storing things in an array, remove the reference to the nth to last node the second time we iterate through the list
26. ### Example: Remove Nth Node from End of List Match :

Are there any special techniques that we can use to help make this easier?  -> We can take a ﬁrst pass through the linked list to store the elements in an array   -> We can also take a pass to get the length of the linked list Plan : What are some different ways we can solve this problem?  1) We can store the elements in an array, get the length of the array, and then rebuild our list excluding the nth to last node  2) We can follow the ﬁrst idea, but instead of storing things in an array, remove the reference to the nth to last node the second time we iterate through the list  What would be the space / time complexity of our solution?
27. ### Example: Remove Nth Node from End of List Our approach:

1) make initial pass through linked list to get length  2) make second pass to remove any reference to nth to last node
28. ### Example: Remove Nth Node from End of List Our approach:

1) make initial pass through linked list to get length  2) make second pass to remove any reference to nth to last node    Let’s draw out how this might work with an example  Given linked list 1->2->3, n = 1 (we want to return 1->2)
29. ### Example: Remove Nth Node from End of List Our approach:

1) make initial pass through linked list to get length  2) make second pass to remove any reference to nth to last node    Let’s draw out how this might work with an example  Given linked list 1->2->3, n = 1 (we want to return 1->2) First pass, we get length = 3. Since n = 1, that means we want to remove the 3 -1 = 2 node (with index starting at 0)
30. ### Example: Remove Nth Node from End of List Our approach:

1) make initial pass through linked list to get length  2) make second pass to remove any reference to nth to last node    Let’s draw out how this might work with an example  Given linked list 1->2->3, n = 1 (we want to return 1->2) First pass, we get length = 3. Since n = 1, that means we want to remove the 3 -1 = 2 node (with index starting at 0)    As we iterate through the list..  at node 1 -> numberOfNodesSeen = 0
31. ### Example: Remove Nth Node from End of List Our approach:

1) make initial pass through linked list to get length  2) make second pass to remove any reference to nth to last node    Let’s draw out how this might work with an example  Given linked list 1->2->3, n = 1 (we want to return 1->2) First pass, we get length = 3. Since n = 1, that means we want to remove the 3 -1 = 2 node (with index starting at 0)    As we iterate through the list..  at node 1 -> numberOfNodesSeen = 0  at node 2-> numberOfNodesSeen = 1
32. ### Example: Remove Nth Node from End of List Our approach:

1) make initial pass through linked list to get length  2) make second pass to remove any reference to nth to last node    Let’s draw out how this might work with an example  Given linked list 1->2->3, n = 1 (we want to return 1->2) First pass, we get length = 3. Since n = 1, that means we want to remove the 3 -1 = 2 node (with index starting at 0)    As we iterate through the list..  at node 1 -> numberOfNodesSeen = 0  at node 2-> numberOfNodesSeen = 1  at node 3 -> numberOfNodesSeen = 2 -> at this point we need to set node 2’s next to be node 3’s next!
33. ### Example: Remove Nth Node from End of List Our approach:

1) make initial pass through linked list to get length  2) make second pass to remove any reference to nth to last node    Let’s draw out how this might work with an example  Given linked list 1->2->3, n = 1 (we want to return 1->2) First pass, we get length = 3. Since n = 1, that means we want to remove the 3 -1 = 2 node (with index starting at 0)    As we iterate through the list..  at node 1 -> numberOfNodesSeen = 0, previousNode = null  at node 2-> numberOfNodesSeen = 1, previousNode = 1  at node 3 -> numberOfNodesSeen = 2, previousNode = 2 -> we set previousNode.next = node.next (node 3’s next = null)
34. ### Group Exercise Work together in groups to practice Matching /

Planning the solution to the problem
35. ### Group Exercise Work together in groups to practice Matching /

Planning the solution to the problem Verbally discuss 2-3 ways to solve the problem and discuss tradeoffs with each approach
36. ### Group Exercise Work together in groups to practice Matching /

Planning the solution to the problem Verbally discuss 2-3 ways to solve the problem and discuss tradeoffs with each approach Think about whether there are any 'magical' helper methods that would greatly help you solve the problem
37. ### Group Exercise Work together in groups to practice Matching /

Planning the solution to the problem Verbally discuss 2-3 ways to solve the problem and discuss tradeoffs with each approach Think about whether there are any 'magical' helper methods that would greatly help you solve the problem Walk through 2 example inputs and explain how the solution would produce the desired output

in it.
40. ### Given a sorted linked list, delete all duplicates such that

each element appear only once.
41. ### Merge two sorted linked lists and return it as a

new list. The new list should be made by splicing together the nodes of the ﬁrst two lists.