貢獻者: 卑鄙葛摟-Babygirl
🧔:interviewer 👶:interviewee
🧔 : Hi,I am your interviewer. Here is the first question. You are given the heads of two sorted linked lists list1
and list2
. Could you merge the two lists in a non-decreasing sorted list?
The list should be made by splicing together the nodes of the first two lists. And return the head of the merged linked list.
👶 : Okay, I have to deal with merging two sorted linked lists into a non-deccreasing sorted linked list. For example, list1=[1,2,4]
,list2=[1,3,4]
, Output=[1,1,2,3,4,4]
.
👶 : I think I will use iterative approach to solve this problem. Use the while loop and iterate through the array comparing the two current node's value. To decide which current node to add to the merge list. So the time complexity is .
🧔 : Remember to consider empty linked lists. You can start to implement.
👶 : First, I check whether list1
and list2
are empty. If one of them is empty, I directly return the other list.
Then maintain a head pointer and a current pointer on the merged linked list.
And by comparing the first nodes of the two linked lists, the smaller node is used as the head of the merged linked list.
I use the while loop to comparing the node of two linked lists sequentially.
For all subsequent nodes in this two lists, I compare the current nodes of the two linked lists,
So I choose the smaller current node and link it to the tail of the merged list, and moving the current pointer of that list to the next node.
After adding a node to the merged linked list, the pointer of the merged list also points to the next node.
I continue this operation when there are some remaining elements in both lists.
Finally, if there are still some elements in only one list, this remaining list is linked to the tail of the merged list.
🧔 : Could you deal with recursive approach?
👶 : Yes, I can use the same concept to find the node with the smaller value in these two lists and splice them into a merged linked list.
🧔 : What is different between recursion and iteration?
👶 : About space, iteration can avoid the waste of space resources. If there are a lot of nodes, recursion may cause stack overflow.
👶 : About efficiency, recursion and iteration are similar.
🧔 : 我會提供你一個指向一個 linked list 的指標叫做 head
,反轉該列表並回傳。
👶 : 好的,我可以使用迭代的方法,在這裡我用到兩個指標來進行反轉,一個指標指向當前節點,另一個指標指向當前節點的前一個節點,當我遍歷該 linked list,依次反轉每一個指標,最後實現整個 linked list 的反轉。
🧔 : 好,你可以開始實作了。
👶 : 時間複雜度為 O(n),空間複雜度為 O(1)。
🧔 : 這邊你有辦法改成用遞迴來實作嗎?
👶 : 可以的,遞迴的方法會不停呼叫函式直到最後一個節點,再將它的 next 指向前一個節點,依序返回直到遞迴結束。
🧔 : 好,你可以開始實作了。
👶 : 在這裡因為使用到 stack 空間的關係,空間複雜度會是 O(n)。
🧔 : 我會提供妳一個包含 n + 1 個整數的整數陣列 nums
,其中每個整數都在 [1, n] 範圍內(含)。而 nums
中只有一個重複的數字,請妳找出這個重複的數字。過程中不能修改 nums
。
👶 : 好的,我的想法是額外使用一個陣列 array
來解決找出重複數字的問題。通過遍歷 nums
,使用額外的 array
來記錄每個元素是否已經被訪問過。
🧔 : 這個方法的空間複雜度會是如何?
👶 : 會是 O(n) 。
🧔 : 請妳換個方法來保證只使用到常數額外空間。
👶 : 因為至少有一個數字重複,所以當遍歷 nums
時一定會進入一個循環。這個問題可以用 cycle detection 的方法來解決,也就是使用兩個指標 slow
和 fast
來追蹤數字,會分成兩步驟,第一個步驟檢查循環的存在,第二個步驟找到循環的起始點,也就是找到重複的數字。
👶 : 在第一步驟我們想要檢查循環的存在,當指標 slow
和 fast
從起點開始,slow
每次走一格,fast
每次走兩格,一直走下去如果它們能相遇,代表存在一個環。反之,若不存在環的話,兩個指標永遠不會相遇。而它們相遇的地點可能是在循環的任何位置,不一定是循環的起始點。
👶 : 在第二步驟我們想要找到循環的起始點,因為環周長減掉循環的起始點至相遇點的距離會等於從起點至環的起始點的距離,也就是說 slow
回到起點並每次走一格, fast
繼續每次走一格,它們再次相遇的點會是循環的起始點,就可以找到重複的數字。
🧔 : 好,你可以開始實作了。
Interviewer
Interviewee