CP2: Topic 5: Linked Lists and Data Structures
Competitive Programming II (Spring 2020)
Dhruv Ramanujan
Example Problems
Homework Problems
LeetCode: Intersection of Two Linked Lists
LeetCode: Intersection of Two Linked Lists
LeetCode: Intersection of Two Linked Lists
Step | A | B |
1 | a1 | b1 |
2 | a2 | b2 |
3 | c1 | b3 |
4 | c2 | c1 |
5 | c3 | c2 |
6 | b1 | c3 |
7 | b2 | a1 |
8 | b3 | a2 |
9 | c1 | c1 |
LeetCode: Intersection of Two Linked Lists
LeetCode: Merge Two Sorted Lists In Place
LeetCode: Merge Two Sorted Lists In Place
LeetCode: Merge Two Sorted Lists In Place
LeetCode: Merge Two Sorted Lists In Place
Pointer A | Pointer B | Current Lists |
2 | 1 | 2->4->5->8 1->6->7 |
2 | 6 | 1->2->4->5->8 6->7 |
4 | 6 | 1->2->4->5->8 6->7 |
5 | 6 | 1->2->4->5->8 6->7 |
8 | 6 | 1->2->4->5->6->7 8 |
8 | 7 | 1->2->4->5->6->7->8 |
LeetCode: Add Two Numbers II
LeetCode: Add Two Numbers II
LeetCode: Add Two Numbers II
Stack A | Stack B |
9 | 5 |
3 | 7 |
1 | 8 |
LeetCode: Add Two Numbers II
Stack A | Stack B | New List | Carry Bit |
9 | 8 | 7 | 1 |
3 | 7 | 1->7 | 1 |
1 | 5 | 7->1->7 | 0 |
LeetCode: Linked List Cycle II
LeetCode: Linked List Cycle II
LeetCode: Linked List Cycle II
Step | Slow Node | Fast Node |
1 | 3 | 3 |
2 | 2 | 0 |
3 | 0 | 2 |
4 | -4 | -4 |
LeetCode: Linked List Cycle II
Slow Pointer: L1 + L2
Fast Pointer: L1 + L2 + NC
LeetCode: Linked List Cycle II
Step | Slow Node | Fast Node | Head |
1 | 3 | 3 | na |
2 | 2 | 0 | na |
3 | 0 | 2 | na |
4 | -4 | -4 | na |
5 | -4 | na | 3 |
6 | 2 | na | 2 |
Homework Hints
Homework Hints: Coconut Splat
Homework Hints: Grandpa Bernie
Homework Hints: Join Strings