1 of 24

CP2: Topic 5: Linked Lists and Data Structures

Competitive Programming II (Spring 2020)

Dhruv Ramanujan

2 of 24

Example Problems

3 of 24

Homework Problems

4 of 24

LeetCode: Intersection of Two Linked Lists

5 of 24

LeetCode: Intersection of Two Linked Lists

  • Problem: Given two linked lists that intersect, return the intersection node.
  • Time Complexity: O(n)
  • Space Complexity: O(1)

6 of 24

LeetCode: Intersection of Two Linked Lists

  • Pointer A points to a1
  • Pointer B points to b1
  • When either pointer reaches the end of the list, move it to the head of the other list

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

7 of 24

LeetCode: Intersection of Two Linked Lists

  • Why does this work?
  • Pointer A traverses: a nodes, b nodes, c nodes
  • Pointer B traverses: a nodes, b nodes, c nodes
  • Since the set of traversed nodes is the same length they intersect at the same time.

8 of 24

LeetCode: Merge Two Sorted Lists In Place

9 of 24

LeetCode: Merge Two Sorted Lists In Place

  • Problem: Given two sorted linked lists merge them together by only changing next pointers. Use as few changes as possible.
  • Time Complexity: O(n)
  • Space Complexity: O(1) extra space

10 of 24

LeetCode: Merge Two Sorted Lists In Place

  • Have a pointer at the start of each list
  • Increment only 1 pointer at a time, starting with the smaller one.
  • When the moving pointer has a larger value than the stationary pointer:
    • Change the links on the list
    • Start moving the other pointer

11 of 24

LeetCode: Merge Two Sorted Lists In Place

  • Have a pointer at the start of each list
  • Increment only 1 pointer at a time, starting with the smaller one.
  • When the moving pointer has a larger value than the stationary pointer:
    • Change the links on the list
    • Start moving the other pointer

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

12 of 24

LeetCode: Add Two Numbers II

13 of 24

LeetCode: Add Two Numbers II

  • Problem: Given 2 linked lists representing 2 integers, add them together into a new linked list.
  • Time Complexity: O(n)
  • Space Complexity: O(n) extra space

14 of 24

LeetCode: Add Two Numbers II

  • Use 2 stacks A, B to store values while traversing the linked list.
  • Pop Values from the stacks and add them.
  • Store an extra variable for carry bits.
  • Add extra node if there is a carry bit at the end!

Stack A

Stack B

9

5

3

7

1

8

15 of 24

LeetCode: Add Two Numbers II

  • Use 2 stacks A, B to store values while traversing the linked list.
  • Pop Values from the stacks and add them.
  • Store an extra variable for carry bits.
  • Add extra node if there is a carry bit at the end!

Stack A

Stack B

New List

Carry Bit

9

8

7

1

3

7

1->7

1

1

5

7->1->7

0

16 of 24

LeetCode: Linked List Cycle II

17 of 24

LeetCode: Linked List Cycle II

  • Problem: Given a Linked List (with a pointer to the head), return the node where the cycle begins.
  • Time Complexity: O(n)
  • Space Complexity: O(1)

18 of 24

LeetCode: Linked List Cycle II

  • Step 1: Detect a cycle
    • Fast Pointer moves 2 steps at a time
    • Slow Pointer moves 1 step at a time
    • If they meet, there is a cycle

Step

Slow Node

Fast Node

1

3

3

2

2

0

3

0

2

4

-4

-4

19 of 24

LeetCode: Linked List Cycle II

  • Step 2: Find Starting Point
    • L1 = distance from head to start of loop
    • L2 = distance from start of loop to meeting point
    • N = number of loops
    • C = Length of loop

Slow Pointer: L1 + L2

Fast Pointer: L1 + L2 + NC

  • 2(L1+L2) = L1+L2+NC
  • L1+L2 = NC
  • L1 = NC - L2
  • L1 = (N-1)C + (C-L2)
  • L1 = [N-1 Loops of C]+ [The distance from the meeting point to the start of the loop]

20 of 24

LeetCode: Linked List Cycle II

  • Step 2: Find Starting Point
    • Create a new pointer “head”
    • Slow increments by 1 node
    • Head increments by 1 node
    • The meeting point is the start of the cycle
  • L1 = 1, Meeting->Start = 1

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

  • L1 = [N-1 Loops of C]+ [The distance from the meeting point to the start of the loop]

21 of 24

Homework Hints

22 of 24

Homework Hints: Coconut Splat

  • Run this like a simulation.

  • Use a list of tuples (players, hand status) to store information
    • Ex: [ (1, “Hands Folded”), (2, “Fist”), (2, “Fist), (3, “Palm Down”) ]

  • When a hand goes behind the back, remove it from the list

  • If the list contains 1 remaining element, they are the winner

23 of 24

Homework Hints: Grandpa Bernie

  • Use an map of arrays (country: [dates]) to store information
    • map[“US”] = [1960, 2000]
    • map[“Iceland”]-->[2000]
    • map[“Greenland”]-->[2016, 2018]

  • Make sure the date lists are stored in sorted order

  • It might help to insert in arbitrary order and sort after all insertions. O(nlogn)

24 of 24

Homework Hints: Join Strings

  • Do not keep appending strings.

  • Use a Linked List with the string as the value stored.

  • Node Data Structure:
    • Value: The string to store
    • Next: Next node it is connected to
    • End: The node at the end of the string of connections (useful for concatenating string of nodes)

  • Only the head nodes need to hold information for the end variable.