Leetcode - Partition List

Paste_Image.png

My code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        if (head == null)
            return null;
        else if (head.next == null)
            return head;
        ListNode dummy = new ListNode(Integer.MIN_VALUE);
        dummy.next = head;
        int total = 0;
        ListNode temp = dummy;
        ListNode tail = null;
        while (temp.next != null) {
            temp = temp.next;
            total++;
        }
        tail = temp;
        
        ListNode preInsert = dummy;
        temp = dummy.next;
        int counter = 0;
        while (counter < total) {
            if (temp.val >= x) {
                if (temp.next == null)
                    break;
                preInsert.next = temp.next;
                tail.next = temp;
                temp.next = null;
                tail = temp;
                temp = preInsert.next;
            }
            else {
                preInsert = temp;
                temp = preInsert.next;
            }
            counter++;
        }
        return dummy.next;
    }
}

My test result:

這道題目類似于快排鏈表的一個小操作鞋屈,換結點。
但是這比插入排序鏈表要爽多了故觅。厂庇。。
因為我這里總是在尾部插入输吏,可以省掉許多考慮权旷。。

早晨起來覺得拖著也是拖著贯溅,就把一家公司的網上筆試做了拄氯。
20道題目躲查,35分鐘。感覺很奇怪译柏。镣煮。。不難鄙麦,但又不簡單典唇,或者說,就是智商題吧胯府。介衔。
不知道做的怎么樣。也沒底盟劫。感覺不差夜牡,也不好。
9.30微軟面試侣签,我得好好準備下了塘装。
想把之前寫的文章全部重看一遍。然后基礎的排序什么的影所,必須搞清楚蹦肴。
今天一定要把CG搞定!:锩洹阴幌!

感覺寫的還是很復雜,實在不能理解卷中,為什么要統(tǒng)計數字矛双。是怕把tail后面的也重復遍歷進去嗎?
我的簡化代碼如下:

public ListNode partition(ListNode head, int x) {
        if (head == null)
            return null;
        else if (head.next == null)
            return head;
        ListNode dummy = new ListNode(Integer.MIN_VALUE);
        dummy.next = head;
        ListNode tail = head;
        while (tail.next != null)
            tail = tail.next;
        ListNode insertTail = tail;
        ListNode pre = dummy;
        ListNode curr = pre.next;
        while (curr != tail) {
            if (curr.val >= x) {
                pre.next = curr.next;
                curr.next = null;
                insertTail.next = curr;
                insertTail = curr;
                curr = pre.next;
            }
            else {
                pre = curr;
                curr = pre.next;
            }
        }
        if (curr.val >= x) {
            if (curr.next == null)
                return dummy.next;
            pre.next = curr.next;
            curr.next = null;
            insertTail.next = curr;
        }
        return dummy.next;
    }

的確需要注意的是蟆豫,不要把tail后面的也給掃描進去了议忽。
所以,到tail為止十减,結束循環(huán)栈幸。然后單獨處理tail就可以了。
不寫了帮辟。明天面試橘忱∠街冢回去復習下簡歷,問答篷角。
好運B瘛!!
**
總結: 快排鏈表
**

Anyway, Good luck, Richardo!

My code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        if (head == null || head.next == null)
            return head;
        ListNode tail = head;
        int counter = 1;
        /** get the tail node of this linked list */
        while (tail.next != null) {
            counter++;
            tail = tail.next;
        }
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode curr = head;
        int i = 0;
        while (i < counter) {
            if (curr.val < x) {
                pre = pre.next;
                curr = curr.next;
            }
            else {
                if (curr == tail)
                    break;
                pre.next = curr.next;
                curr.next = null;
                tail.next = curr;
                tail = curr;
                curr = pre.next;
            }
            i++;
        }
        return dummy.next;   
    }
}

這次寫的感覺比以前簡單一些。蒿赢。
然后我的思路,和剛剛有道題目的第二種解法一樣渣触。

  1. Odd Even Linked List
    http://www.reibang.com/p/c4a424042667

這道題木,第二種解法壹若,就是掃描鏈表嗅钻。碰到偶數的,就把他直接插到鏈表后部店展。
這道題木也一樣养篓。碰到 >= x的,直接把他插到鏈表后部赂蕴。
剛剛那道題目柳弄,采用了一個ListNode endTag來防止重復掃描的問題,即把tail后面概说,后來插入的結點也給掃描了碧注。
這道題木我換了一種做法。直接做一個計數器記錄結點總個數糖赔。
然后while循環(huán)就用這個做判斷萍丐。會簡單很多。
當然放典,這道題木逝变,需要判斷 curr == tail
如果等于的話,我的當前代碼是不能把curr 插入到自己后面的奋构。直接break就行壳影。
這個錯誤,也犯過好幾次弥臼。就是自己對自己進行處理宴咧。處理不了,出錯醋火。以后要注意悠汽。

Anyway, Good luck, Richardo!

My code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        if (head == null || head.next == null) {
            return head;
        }
        
        ListNode smallHead = new ListNode(-1);
        ListNode bigHead = new ListNode(-1);
        ListNode small = smallHead;
        ListNode big = bigHead;
        while (head != null) {
            if (head.val < x) {
                small.next = head;
                small = small.next;
            }
            else {
                big.next = head;
                big = big.next;
            }
            head = head.next;
        }
        
        small.next = bigHead.next;
        big.next = null;
        return smallHead.next;
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        ListNode n1 = new ListNode(2);
        ListNode n2 = new ListNode(1);
        n1.next = n2;
        test.partition(n1, 2);
    }
}

我的做法挺復雜,也沒做出來芥驳。最后估計可以debug出來柿冲,但corner case 太多。
這個做法比較簡潔易懂兆旬,關注點在于:
big.next = null; 必須切斷假抄。否則就是一個環(huán),無限循環(huán)。
reference:
https://discuss.leetcode.com/topic/7005/very-concise-one-pass-solution/5

特點在于宿饱,用了雙dummy node
然后想到 odd even linked list 應該也可以這么做
http://www.reibang.com/p/c4a424042667

Anyway, Good luck, Richardo! --- 08/17/2016

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末熏瞄,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子谬以,更是在濱河造成了極大的恐慌强饮,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件为黎,死亡現場離奇詭異邮丰,居然都是意外死亡,警方通過查閱死者的電腦和手機铭乾,發(fā)現死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門剪廉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人炕檩,你說我怎么就攤上這事斗蒋。” “怎么了笛质?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵泉沾,是天一觀的道長。 經常有香客問我妇押,道長爆哑,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任舆吮,我火速辦了婚禮揭朝,結果婚禮上,老公的妹妹穿的比我還像新娘色冀。我一直安慰自己潭袱,他們只是感情好,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布锋恬。 她就那樣靜靜地躺著屯换,像睡著了一般。 火紅的嫁衣襯著肌膚如雪与学。 梳的紋絲不亂的頭發(fā)上彤悔,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天,我揣著相機與錄音索守,去河邊找鬼晕窑。 笑死,一個胖子當著我的面吹牛卵佛,可吹牛的內容都是我干的杨赤。 我是一名探鬼主播敞斋,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼疾牲!你這毒婦竟也來了植捎?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤阳柔,失蹤者是張志新(化名)和其女友劉穎焰枢,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體舌剂,經...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡医咨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了架诞。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡干茉,死狀恐怖谴忧,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情角虫,我是刑警寧澤沾谓,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站戳鹅,受9級特大地震影響均驶,放射性物質發(fā)生泄漏。R本人自食惡果不足惜枫虏,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一妇穴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧隶债,春花似錦腾它、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至赞警,卻和暖如春妓忍,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背愧旦。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工世剖, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人笤虫。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓搁廓,卻偏偏與公主長得像引颈,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子境蜕,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359

推薦閱讀更多精彩內容

  • My code: 這道題目不難蝙场,但是很煩。自己也沒能做出來粱年。剛剛才分析過插入排序的售滤,但是一旦涉及到鏈表,就好復雜台诗。...
    Richardo92閱讀 469評論 0 1
  • My code: 一開始我覺得完箩,這道題目簡單嗎?我寫了有點時間拉队,也不是一次性過弊知。我的做法就是現場直接進行交換,一遍...
    Richardo92閱讀 681評論 0 0
  • My code: My test result: 這道題目不難粱快,感覺就是用二叉搜索法將鏈表改造成balanced ...
    Richardo92閱讀 322評論 0 1
  • My code: My test result: 這次作業(yè)說實話不難秩彤,但是要完全寫出來還是有點細節(jié)的。首先事哭,一開始...
    Richardo92閱讀 458評論 1 1
  • My code: 最近比較忙漫雷。之前寫的題目一直沒有更新。現在正好有點時間鳍咱,更新一下降盹。這道題木之前聽學長說過,他在面...
    Richardo92閱讀 290評論 0 0