貢獻者: 屎地芬森-Stevenson
模擬面試錄影: 中+英
👹 : interviewer
👾 : interviewee
👹 : 屎地芬森先生你好,給你兩個linked list, 節點的順序都依照節點裡面的值排序好了, 請把它們合併然後回傳給我合併後的linked list head, 可以闡述你打算使用的解答做法, 然後實作
👾 : 我們先預設這個題目是升序排列的, 我的工作會是需要把list1, list2
給合併成mergeList
並且回傳list head
首先我的相法認為我們可以利用merge sort當中merge的做法來處理
一開始我會先定義一個結構體struct node
如下
👾 : 這裡使用的linked list是singly linked list
👾 : 接著考慮兩種特例也就是list1
或list2
其中一個為空, 或者兩者都為空的時候, 直接回傳即可
👾 : 接下來處理一般情況可以遞迴的處理, 每次挑出要加入mergeHead
的node後, 把剩下的linked list繼續傳入merge
當中處理
👹 : cur_node1, cur_node2
是否有存在的必要?另外可以分析時間複雜度嗎?
👾 : 面試官說的沒錯, 確實不需要使用cur_node1, cur_node2
就可以完成此算法, 改寫如下
👾 : 計算時間複雜度首先可以先列出遞迴式, 假設這個merge
function的input size是, 時間複雜度可以表示, 進行recursive call的時間複雜度會是, 其他部分的操作都是, 所以遞迴式可以表示成, 解這個遞迴式可以得到
👹 : 可以優化此算法嗎?
👾 : 可以, 原本我沒有使用到編譯器的tail recursion特性導致遞迴呼叫之後會讓function stack不斷疊加, 我可以嘗試把程式改寫成non-recursive
👾 : (使用測資測試…)
👹 : 屎地芬森先生謝謝你的作答, 答的不錯謝謝你
👹 : 屎地芬森先生您好, 歡迎來到我們公司的面試, 我們給你一個問題, 給你一個linked list, 當中的值是隨機的, 希望你把節點兩兩交換, 並且不能直接修改node當中的值, 請你闡述你的解法之後再開始實作
👾 : 謝謝面試官,這個題目會提供給我一個linked list,之後把節點兩兩交換並且不能直接改變節點中的值
👾 : 首先我會先定義linked list的節點結構
👾 : 再來思考兩個特別情況, 如果給我的linked list為空, 或者只有一個節點,那也沒有節點可以互換, 直接回傳即可
👾 : 再來處理一般情況, 使用cnode
和nnode
, nnode
是cnode
的下一個節點, 每次互換的時候把cnode->next
指向原本的nnode->next
, 然後把nnode->next
指向cnode
👹 : 可以使用一些測資確認正確性嗎?
👾 : (測試…) 不好意思面試官, 有發現錯誤的部分, 請給我一點時間思考並修正
👹 : 沒問題
👾 : 謝謝面試官給我時間思考並修正, 我想可以利用一個指標來記錄前一組pairs的cnode
, 因為錯誤的原因是cnode->next
原本應該指向下一組的nnode
, 但在我原本的做法中卻只會指向下一組的cnode
, 所以我利用pnode
來記錄上一組的cnode
並把pnode->next
指向當前的nnode
👹 : 可以請你計算時間複雜度嗎?
👾 : 因為while loop在linked list上面走訪的時候不會往回走, 每個節點都只會走訪一次, 如果給定的linked list長度是, 則時間複雜度就會是
👹 : 屎地芬森先生謝謝你的作答
👹 : Hello Mr.Stevenson, welcome to our company's interview. We have a question for you, you can describe your solution first and solve it. We're going to give you a sorted linked list with some duplicate values in it, please remove the duplicate nodes and make sure every value exists only once
👾 : Hello interviewer, I'm happy to be interviewed by your company and you. You're going to give me a head of a sorted linked list, and I'll have to remove all the duplicates, I want to make sure that I can assume the sorted order is ascending order right? and I still need remain the duplicate value in one node, not remove all of it right?
👹 : Yes your assumption is right, you can continue your answer
👾 : First I have to define a structure of the linked list node
👾 : I have to deal with speical case first, if you give me an empty linked list or a linked list with a single node. In both cases I'll just return the head back to you
👾 : For common cases, I'll use two pointer to struct node
, cnode, nnode
, and I'll compare the value of them like the following. Since we'll never remove the head
node, the original head
is still going to be the final head
so we can just return head
👹 : Can you calculate the time complexity of your algorithm? and I wonder that if you can use only one pointer to solve this problem?
👾 : I'll change my code using just one pointer first, becauze nnode
is always cnode->next
, so we can replace nnode
👾 : For the time complexity, assume the given linked list size is , for each iteration in the while loop, the time complexity is , and we'll traverse each node exactly once, so the total time complexity is
👹 : Thank you Mr. Stevenson. I've noticed that you remove the duplicate node by changing cnode->next
, but the duplicate nodes are still alive in the system, can you try to delete it?
👾 : I know I didn't actually free the memory space of the duplicates node, I'll call these nodes as garbage node, and delete it like the following
👹 : Thank you very much Mr. Steveson.