SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Linked List Interview Prep
Spencer Moran
11/20/13
Why Linked Lists?
• Simplicity
• Little variation
• Pointers and references
Singly Linked List

Almost all LL interview questions are based on singly linked lists
Doubly Linked List
Implementation
• Can be implemented as
an ADT
• Often use templates
• List is referred to by the
head, the first node in
the list
• If you modify the
list, you must return the
head of the new list
Traversal
Insertion
Deletion
Linked List Question Strategies
• Use two (or more??) pointers to traverse the
list
• Traverse pointers at different speeds
• Use an additional data structure
Common Linked List Questions

Find the kth to last element in a singly linked list.
Strategy
• Use multiple pointers to traverse the list.
• Return an error if the list is not at least k
nodes long.
• Traverse the list with 2 pointers:
– Pointer1 starts at head
– Pointer2 starts k nodes in front of the head.
– When pointer2 reaches the end of the list, return
pointer1
Common Linked List Questions
Determine if a singly linked list has a loop or cycle.
Strategy
• Use multiple pointers and traverse them at
different speeds.
• Start both pointers at head:
– Traverse the list, moving fast pointer twice as fast
as the slow pointer.
– If fast pointer reaches the end of the list, there is
no cycle.
– If the pointers ever are equal to one
another, there is a cycle.
BONUS QUESTION
Determine if a linked list has a cycle. If it
does, return the element where the cycle
begins.
Bonus Question #2

Find the length of the cycle without traversing it
multiple times.
Common Linked List Questions

Reverse a singly linked list.
Strategy
• Use an additional data structure.
• Traverse the list, pushing each node into the
stack.
• Set the new head equal to the first element in
the list.
• Starting at the new head, add the top element
from the stack until the stack is empty.
Other common questions
• Remove duplicates from a singly linked list
• Merge sort
• Remove element from middle of list (without
head)
• Check if the linked list is a palindrome
• Fold a singly linked list using no containers
Meeting Summary
We also covered a bunch of questions at the end
of the meeting. These questions were mostly
about linked lists, but we also started covering
trees and decided that trees and graphs will be
our next topic. The date of our next meeting will
be arranged through the facebook group.

Weitere ähnliche Inhalte

Was ist angesagt? (9)

Problem solving using truth table
Problem solving using truth tableProblem solving using truth table
Problem solving using truth table
 
Data structure unit I part B
Data structure unit I part BData structure unit I part B
Data structure unit I part B
 
LINEAR SEARCH
LINEAR SEARCHLINEAR SEARCH
LINEAR SEARCH
 
Elements, Compounds & Mixtures Day 3
Elements, Compounds & Mixtures Day 3Elements, Compounds & Mixtures Day 3
Elements, Compounds & Mixtures Day 3
 
Trees
TreesTrees
Trees
 
Link list 2
Link list 2Link list 2
Link list 2
 
Chapter 5 searching and sorting handouts with notes
Chapter 5   searching and sorting handouts with notesChapter 5   searching and sorting handouts with notes
Chapter 5 searching and sorting handouts with notes
 
Itmat pcbi-r-course-1
Itmat pcbi-r-course-1Itmat pcbi-r-course-1
Itmat pcbi-r-course-1
 
Learning With Complete Data
Learning With Complete DataLearning With Complete Data
Learning With Complete Data
 

Ähnlich wie Linked lists

Data Structures and Algorithms - Lec 05.pptx
Data Structures and Algorithms - Lec 05.pptxData Structures and Algorithms - Lec 05.pptx
Data Structures and Algorithms - Lec 05.pptxRameshaFernando2
 
arrangement of the periodic table for cornell notes
 arrangement of the periodic table for cornell notes arrangement of the periodic table for cornell notes
arrangement of the periodic table for cornell noteslaheflin
 
Top 10 LinkedList Interview Questions 2023 [MAANG]
Top 10 LinkedList Interview Questions 2023 [MAANG]Top 10 LinkedList Interview Questions 2023 [MAANG]
Top 10 LinkedList Interview Questions 2023 [MAANG]Simplilearn
 
data structures and applications power p
data structures and applications power pdata structures and applications power p
data structures and applications power pMeghaKulkarni27
 
mbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptxmbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptxjotaro11
 
LinkedList Presentation.pptx
LinkedList Presentation.pptxLinkedList Presentation.pptx
LinkedList Presentation.pptxwahid431192
 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfKamranAli649587
 
Linear Data Structures - List, Stack and Queue
Linear Data Structures - List, Stack and QueueLinear Data Structures - List, Stack and Queue
Linear Data Structures - List, Stack and QueueSelvaraj Seerangan
 
Different types of Linked list.
Different types of Linked list.Different types of Linked list.
Different types of Linked list.JAYANTAOJHA
 
Reverse a Linked List In Place in Python
Reverse a Linked List In Place in PythonReverse a Linked List In Place in Python
Reverse a Linked List In Place in PythonKal Bartal
 

Ähnlich wie Linked lists (20)

Linked List
Linked ListLinked List
Linked List
 
Data Structures and Algorithms - Lec 05.pptx
Data Structures and Algorithms - Lec 05.pptxData Structures and Algorithms - Lec 05.pptx
Data Structures and Algorithms - Lec 05.pptx
 
2- link-list.ppt
2- link-list.ppt2- link-list.ppt
2- link-list.ppt
 
arrangement of the periodic table for cornell notes
 arrangement of the periodic table for cornell notes arrangement of the periodic table for cornell notes
arrangement of the periodic table for cornell notes
 
Link_List.pptx
Link_List.pptxLink_List.pptx
Link_List.pptx
 
Top 10 LinkedList Interview Questions 2023 [MAANG]
Top 10 LinkedList Interview Questions 2023 [MAANG]Top 10 LinkedList Interview Questions 2023 [MAANG]
Top 10 LinkedList Interview Questions 2023 [MAANG]
 
unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
data structures and applications power p
data structures and applications power pdata structures and applications power p
data structures and applications power p
 
mbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptxmbit_Unit-2_Linked List.pptx
mbit_Unit-2_Linked List.pptx
 
LinkedList Presentation.pptx
LinkedList Presentation.pptxLinkedList Presentation.pptx
LinkedList Presentation.pptx
 
Linked list
Linked listLinked list
Linked list
 
Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdf
 
Linear Data Structures - List, Stack and Queue
Linear Data Structures - List, Stack and QueueLinear Data Structures - List, Stack and Queue
Linear Data Structures - List, Stack and Queue
 
Different types of Linked list.
Different types of Linked list.Different types of Linked list.
Different types of Linked list.
 
Sy ds -I
Sy ds -ISy ds -I
Sy ds -I
 
Sy ds -I
Sy ds -ISy ds -I
Sy ds -I
 
Sy ds i
Sy ds iSy ds i
Sy ds i
 
Reverse a Linked List In Place in Python
Reverse a Linked List In Place in PythonReverse a Linked List In Place in Python
Reverse a Linked List In Place in Python
 
lecture 02.2.ppt
lecture 02.2.pptlecture 02.2.ppt
lecture 02.2.ppt
 

Kürzlich hochgeladen

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 

Kürzlich hochgeladen (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 

Linked lists

  • 1. Linked List Interview Prep Spencer Moran 11/20/13
  • 2. Why Linked Lists? • Simplicity • Little variation • Pointers and references
  • 3. Singly Linked List Almost all LL interview questions are based on singly linked lists
  • 5. Implementation • Can be implemented as an ADT • Often use templates • List is referred to by the head, the first node in the list • If you modify the list, you must return the head of the new list
  • 9. Linked List Question Strategies • Use two (or more??) pointers to traverse the list • Traverse pointers at different speeds • Use an additional data structure
  • 10. Common Linked List Questions Find the kth to last element in a singly linked list.
  • 11. Strategy • Use multiple pointers to traverse the list. • Return an error if the list is not at least k nodes long. • Traverse the list with 2 pointers: – Pointer1 starts at head – Pointer2 starts k nodes in front of the head. – When pointer2 reaches the end of the list, return pointer1
  • 12.
  • 13. Common Linked List Questions Determine if a singly linked list has a loop or cycle.
  • 14. Strategy • Use multiple pointers and traverse them at different speeds. • Start both pointers at head: – Traverse the list, moving fast pointer twice as fast as the slow pointer. – If fast pointer reaches the end of the list, there is no cycle. – If the pointers ever are equal to one another, there is a cycle.
  • 15.
  • 16. BONUS QUESTION Determine if a linked list has a cycle. If it does, return the element where the cycle begins.
  • 17.
  • 18. Bonus Question #2 Find the length of the cycle without traversing it multiple times.
  • 19. Common Linked List Questions Reverse a singly linked list.
  • 20. Strategy • Use an additional data structure. • Traverse the list, pushing each node into the stack. • Set the new head equal to the first element in the list. • Starting at the new head, add the top element from the stack until the stack is empty.
  • 21.
  • 22. Other common questions • Remove duplicates from a singly linked list • Merge sort • Remove element from middle of list (without head) • Check if the linked list is a palindrome • Fold a singly linked list using no containers
  • 23. Meeting Summary We also covered a bunch of questions at the end of the meeting. These questions were mostly about linked lists, but we also started covering trees and decided that trees and graphs will be our next topic. The date of our next meeting will be arranged through the facebook group.

Hinweis der Redaktion

  1. Each node has a pointer to the next node and the previous node. Not seen in interviews because they trivialize many problems