Upcoming SlideShare
×

385 views
317 views

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
385
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
10
0
Likes
1
Embeds 0
No embeds

No notes for slide
• Each node has a pointer to the next node and the previous node. Not seen in interviews because they trivialize many problems

1. 1. Linked List Interview Prep Spencer Moran 11/20/13
2. 2. Why Linked Lists? • Simplicity • Little variation • Pointers and references
3. 3. Singly Linked List Almost all LL interview questions are based on singly linked lists
5. 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
6. 6. Traversal
7. 7. Insertion
8. 8. Deletion
9. 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. 10. Common Linked List Questions Find the kth to last element in a singly linked list.
11. 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. 12. Common Linked List Questions Determine if a singly linked list has a loop or cycle.
13. 13. 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.
14. 14. BONUS QUESTION Determine if a linked list has a cycle. If it does, return the element where the cycle begins.
15. 15. Bonus Question #2 Find the length of the cycle without traversing it multiple times.