Linked List 面接の質問と回答
質問 11. Implement a function to reverse a singly linked list.
To reverse a linked list, you can iterate through the list and reverse the direction of pointers.
Example:
Input: 1 -> 2 -> 3 -> 4 -> 5
Output: 5 -> 4 -> 3 -> 2 -> 1
役に立ちましたか?
コメントを追加
コメントを見る
質問 12. Detect a cycle in a linked list.
You can use Floyd's cycle-finding algorithm, also known as the 'tortoise and hare' algorithm.
Example:
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 2 (cycle)
Output: True
役に立ちましたか?
コメントを追加
コメントを見る
質問 13. Merge two sorted linked lists into a single sorted list.
Traverse both lists simultaneously, compare elements, and merge them into a new sorted list.
Example:
Input: List1: 1 -> 3 -> 5, List2: 2 -> 4 -> 6
Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6
役に立ちましたか?
コメントを追加
コメントを見る
質問 14. Find the kth to last element of a singly linked list.
Use two pointers; one moves k nodes into the list, and then both iterate until the second one reaches the end.
Example:
Input: 1 -> 2 -> 3 -> 4 -> 5, k = 2
Output: 4
役に立ちましたか?
コメントを追加
コメントを見る
質問 15. Remove duplicates from an unsorted linked list.
Use a hash set to keep track of unique elements while traversing the list.
Example:
Input: 1 -> 2 -> 2 -> 3 -> 4 -> 4 -> 5
Output: 1 -> 2 -> 3 -> 4 -> 5
役に立ちましたか?
コメントを追加
コメントを見る
ユーザー評価で最も役立つ内容: