1 of 22

Linked Lists

2 of 22

linktr.ee/bingacm

3 of 22

How to Store Data?

  • Put all in one place, order sequentially
    • Arrays!
    • Static memory
    • Easy to search and reverse
    • Difficult to add/remove elements
  • What if we don’t know how many to add? Lots of anticipated changes ?
    • Make memory dynamic, so elements are not close together
    • Each element knows where the next element is located

4 of 22

Linking Elements (Single)

  • Start with one element at a specific address (i.e. 0xABCD)
  • Think of each element as a flashcard, containing two things:
    • It’s data (a string, an int, a char, etc.)
    • The address of another element (0x####)
  • Last element contains NULL instead of next address
  • Head = first

5 of 22

Linked Lists

  • Two basic types of linked lists
    • Singly Linked
    • Doubly Linked
  • A few more versions exist, different uses
  • Advantages:
    • Adding, Deletion, Memory, Dynamic
  • Disadvantages:
    • Searching, Organization, Reversal �

6 of 22

Activity Break (Link, Add, Delete)

7 of 22

Why Linked Lists ?

  • Problems where adding and deleting happen MORE than searching for a specific element!
  • Dynamic saves memory with changes

8 of 22

Which Linked Lists?

  • Singly Linked
  • Single + Tail
  • Single + Circular
  • Doubly Linked

9 of 22

Activity Break (Searching, Reversal)

10 of 22

Coding: Node Class

11 of 22

Coding: Linked List Class

12 of 22

Applications for Linked Lists

Doubly Linked Lists

Web Browsers

Singly Linked Lists

To-do Lists

Doubly Linked Lists

Ctrl-z (undo/redo)

Singly Linked Lists

Playlists

Doubly Linked Lists

Locations in GPS

13 of 22

LeetCode: Problem 2�

Add Two Numbers

14 of 22

342 + 465 = 807

15 of 22

Solution (c++)

16 of 22

�LeetCode: Problem 21

Merge 2 Sorted Linked Lists

17 of 22

18 of 22

Solution (in Java)

19 of 22

LeetCode: Problem 725

Split Linked List into parts

20 of 22

21 of 22

Solution (c++)

22 of 22

Thanks for coming :)