SlideShare une entreprise Scribd logo
1  sur  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.

Contenu connexe

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
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
 
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
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

Dernier (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
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...
 
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
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

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.

Notes de l'éditeur

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