某司測試工程師面試:鏈表相關

dsa_linkedlist

在兩家公司面試時被均被考核到鏈表廊佩,具體問題如下:

  1. 鏈表和順序表有什么區(qū)別?
  2. 給定一個鏈表如何判斷是循環(huán)鏈表靖榕?

因為面的是測試崗标锄,所以要求不高。
對于問題1茁计,參考 stackoverflow 的回答:


stackoverflow:When to use LinkedList over ArrayList?

針對其中的部分描述料皇,編寫了測試代碼進行驗證:

package testing.interview;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;

/**
 * Print the time of linkedList and arrayList by add element 100000 times.
 * 
 * @author lishoujun https://github.com/lishoujun/java-learn
 */
public class LinkedListAdd {
    public static void main(String[] args) {
        final int size = 100000;
        LinkedList<Integer> linkedList = new LinkedList<>();
        long linkedListAddStartTime = new Date().getTime();
        System.out.println(linkedListAddStartTime);
        for (int i = 0; i < size; i++) {
            linkedList.add(0, i);
        }
        long linkedListAddEndTime = new Date().getTime();
        System.out.println("linkedListTime:" + (linkedListAddEndTime - linkedListAddStartTime));

        ArrayList<Integer> arrayList = new ArrayList<>();
        long arrayListStartTime = new Date().getTime();
        for (int i = 0; i < size; i++) {
            arrayList.add(0, i);
        }
        long arrayListEndTime = new Date().getTime();

        System.out.println("arrayListTime:" + (arrayListEndTime - arrayListStartTime));

        // for debug
        try {
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

對于問題2

package testing.interview;

/**
 * A hasCycle function to check is there a Cycle in LinkedList.
 * the LinkedList is a simple edition just has an int data and a Node as next.
 * 
 * @author lishoujun https://github.com/lishoujun/java-learn
 */
public class LinkedListCycle {
    public static void main(String[] args) {
        System.out.println(hasCycle(null));
        System.out.println(hasCycle(new Node(1, null)));
        System.out.println(hasCycle(new Node(1, new Node(1, null))));
        Node first = new Node(1, null);
        first.next = first;
        System.out.println(hasCycle(first));
        Node second = new Node(1, first);
        first.next = second;
        System.out.println(hasCycle(first));
        /**
         * result:
         * false 
         * false 
         * false 
         * true 
         * true
         */
    }

    public static boolean hasCycle(Node start) {
        if (start == null || start.next == null)
            return false;
        Node tmp = start;
        Node current = start.next;
        while (current.next != null) {
            if (tmp == current) {
                return true;
            }
            current = current.next;
        }
        return false;
    }
}

class Node {
    int data;
    Node next;

    Node() {
    }

    Node(int data, Node next) {
        this.data = data;
        this.next = next;
    }
}

參考資料,排名代表推薦程度

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末星压,一起剝皮案震驚了整個濱河市践剂,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌娜膘,老刑警劉巖逊脯,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異竣贪,居然都是意外死亡军洼,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門演怎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來匕争,“玉大人,你說我怎么就攤上這事爷耀「噬#” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵歹叮,是天一觀的道長跑杭。 經(jīng)常有香客問我,道長咆耿,這世上最難降的妖魔是什么艘蹋? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮票灰,結果婚禮上女阀,老公的妹妹穿的比我還像新娘宅荤。我一直安慰自己,他們只是感情好浸策,可當我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布冯键。 她就那樣靜靜地躺著,像睡著了一般庸汗。 火紅的嫁衣襯著肌膚如雪惫确。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天蚯舱,我揣著相機與錄音改化,去河邊找鬼。 笑死枉昏,一個胖子當著我的面吹牛陈肛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播兄裂,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼句旱,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了晰奖?” 一聲冷哼從身側響起谈撒,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎匾南,沒想到半個月后啃匿,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡蛆楞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年立宜,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片臊岸。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡橙数,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出帅戒,到底是詐尸還是另有隱情灯帮,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布逻住,位于F島的核電站钟哥,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏瞎访。R本人自食惡果不足惜腻贰,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望扒秸。 院中可真熱鬧播演,春花似錦冀瓦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至洲炊,卻和暖如春感局,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背暂衡。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工询微, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人狂巢。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓撑毛,卻偏偏與公主長得像,于是被迫代替她去往敵國和親隧膘。 傳聞我的和親對象是個殘疾皇子代态,可洞房花燭夜當晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容