日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【IT笔试面试题整理】链表

發布時間:2023/12/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【IT笔试面试题整理】链表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如何準備

Linked list questions are extremely common These can range from simple (delete a node ina linked list) to much more challenging Either way, we advise you to be extremely comfortable with the easiest questions Being able to easily manipulate a linked list in the simplestways will make the tougher linked list questions much less tricky With that said, we present some “must know” code about linked list manipulation You should be able to easily writethis code yourself prior to your interview

鏈表是面試中非常常見的問題。問題難度有難有易。無論題目如何,我們都建議你先練熟簡單題目。掌握了簡單鏈表題,面對那些難題的時就會輕松一點。所以下面列出了一些鏈表題中必備的代碼。

Creating a Linked List:

創建一個鏈表:

NOTE: When you’re discussing a linked list in an interview, make sure to understand whether it is a single linked list or a doubly linked list.
1 class Node {2 Node next = null;3 int data;4 public Node(int d) { data = d; }5 void appendToTail(int d) {6 Node end = new Node(d);7 Node n = this;8 while (n.next != null) { n = n.next; }9 n.next = end;10 }11 }

注意:在面試的時候寫鏈表,一定要先問清楚是寫單鏈表還是雙鏈表!

Deleting a Node from a Singly Linked List

在單鏈表中刪除一個節點

1 Node deleteNode(Node head, int d) {2 Node n = head;3 if (n.data == d) {4 return head.next; /* moved head */5 }6 while (n.next != null) {7 if (n.next.data == d) {8 n.next = n.next.next;9 return head; /* head didn’t change */10 }11 n = n.next;12 }13 }

2 1 Write code to remove duplicates from an unsorted linked list;
FOLLOW UP
How would you solve this problem if a temporary buffer is not allowed

2.1從一個未排序的鏈表中刪除重復元素
進階:如果不能使用額外的空間怎么辦?
2.1解答:
如果可以使用哈希表的話,那么檢查每一個元素是否重復,然后移除。

如果不能額外使用空間的話,我們就需要兩個指針來遍歷數組:current用來正常的遍歷;runner則用來檢查元素是否重復。runner只為一個元素檢查一次是否重復,因為每一次runner會刪除和元素的所有重復元素。

2.2 Implement an algorithm to find the nth to last element of a singly linked list

2.2 實現算法查找單鏈表中的倒數第n個元素。
注意:這個問題強烈的暗示遞歸算法,但是這里才采用一種更巧妙的方法。像這樣類似的問題就,一定要多思考看看。能不能用非遞歸的方法代替遞歸的方法。
2.2解答:
這里我們假設鏈表的長度至少為n。
算法描述:
(1) 創建兩個指針p1和p2,均指向鏈表頭。
(2) 讓p2增加n-1次,使得p2指向從鏈表頭第n個元素(使得p1和p2之間距離為n)
(3) 若p2->next為空,則返回p1;不為空則同時增加p1,p2。(如果p2->next為空,則表示p1所指的元素為倒數第n個,因為p1,p2的距離為n。)
(4) 重復(3)

2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node
EXAMPLE?Input: the node ‘c’ from the linked list a->b->c->d->e??Result: nothing is returned, but the new linked list looks like a->b->d->e

2.3 實現一個算法,只給你鏈表中間的一個元素(沒有鏈表頭),將其從鏈表中刪除。
例如:
輸入:節點 c (原鏈表為 a->b->c->d->e)
輸出:沒有任何返回值。但鏈表變成 a->b->d->e
2.3解答:
本題的解答只是把輸入的下一個元素拷貝到輸入的這個元素中以完成刪除輸入的元素。(譯者注:原文的代碼沒有釋放下一元素的空間。)
注意:這樣的方法并不能刪除鏈表的最后一個元素。這一點需要和你的面試官說清楚。算法有缺陷沒有關系,大膽的告訴你的面試官,他們喜歡看到你提出這些。至于怎么解決可以和你的面試官討論。

2.4 You have two numbers represented by a linked list, where each node contains a single digit The digits are stored in reverse order, such that the 1’s digit is at the head of the list Write a function that adds the two numbers and returns the sum as a linked list??EXAMPLE Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)??Output: 8 -> 0 -> 8

2.4 兩個用鏈表表示的數字,最低位在表頭。寫一個函數對兩個這樣的鏈表求和,采用相同的形式返回結果。
例如: (3 -> 1 -> 5) + (5 -> 9 -> 2)? 返回??8 -> 0 -> 8
2.4解答:
本題可以采用遞歸的方式逐位的做加法。算法流程:

2

2 5 Given a circular linked list, implement an algorithm which returns node at the begin- ning of the loop??DEFINITION Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked list??EXAMPLE??input: A -> B -> C -> D -> E -> C [the same C as earlier]??output: C
solution:
If we move two pointers, one with speed 1 and another with speed 2, they will end up meeting if the linked list has a loop?? Why?? Think about two cars driving on a track—the faster car will always pass the slower one! The tricky part here is finding the start of the loop?? Imagine, as an analogy, two people racing around a track, one running twice as fast as the other?? If they start off at the same place, when will they next meet?? They will next meet at the start of the next lap
Now, let’s suppose Fast Runner had a head start of k meters on an n step lap When will
they next meet?? They will meet k meters before the start of the next lap?? (Why? Fast Runner would have made k + 2(n - k) steps, including its head start, and Slow Runner would have made n - k steps?? Both will be k steps before the start of the loop )
Now, going back to the problem, when Fast Runner (n2) and Slow Runner (n1) are moving around our circular linked list, n2 will have a head start on the loop when n1 enters?? Specifically, it will have a head start of k, where k is the number of nodes before the loop?? Since n2 has a head start of k nodes, n1 and n2 will meet k nodes before the start of the loop?
So, we now know the following:
1???? Head is k nodes from LoopStart (by definition)?
2???? MeetingPoint for n1 and n2 is k nodes from LoopStart (as shown above)?
Thus, if we move n1 back to Head and keep n2 at MeetingPoint, and move them both at the same pace, they will meet at LoopStart?

2.5 給一個帶有環的鏈表,設計算法查找這個環的起點。
環的定義:鏈表中的某一元素的下一個元素為之前的一個元素。
例如:輸入 A -> B -> C -> D -> E -> C 輸出 C
2.5 解答:
如果在鏈表中有兩個指針,從表頭開始p1以每次一個節點的速度前進,p2每次2個。那么這兩個指針一定在鏈表的環中相遇。想想兩輛車以不同的速度在一個環形跑道上運動,那么他們肯定會在跑道上再次相遇。
這個問題最難的部分就在于如何超出這個環開始的節點。假想下如果是在一個圓形的跑道上p2以兩倍的速度于p1同時從起點出發,他們將會哪里相遇。還是在起點!
現在我們再假設如果p2超前起跑線k米起跑,他們第一次在哪里相遇呢?在起跑線后面k米處。(為什么?假設p2過了起跑線之后跑了x和p1相遇,那么p2跑了l-k+x,其中l為跑道的長度,至相遇時p1也跑了x。根據相遇時消耗的時間相等得方程 (l-k-x)/2=x/1,解得 x = l-k,也就是在起跑線后k處相遇。)

那重新回到問題本身,當p1指針剛進入環的時候,p2在環中恰好領先p1指正k個節點,k為鏈表頭節點到環開始處的距離。根據之前的結論,當p1、p2相遇的時候,他們都相遇環開始節點k個距離。
那么我們得到如下結論:
(1)表頭距離環開始處k節點
(2)p1,p2距離環開始處也是k個節點。
如果當p1、p2相遇之后,將p1至于表頭然后兩個指針都采用1的速度移動的話。那么相遇處即為環開始處。

總結

以上是生活随笔為你收集整理的【IT笔试面试题整理】链表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。