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搞定!!!

感觉写的还是很复杂,实在不能理解,为什么要统计数字。是怕把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为止,结束循环。然后单独处理tail就可以了。
不写了。明天面试。回去复习下简历,问答。
好运!!!
**
总结: 快排链表
**

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.jianshu.com/p/c4a424042667

这道题木,第二种解法,就是扫描链表。碰到偶数的,就把他直接插到链表后部。
这道题木也一样。碰到 >= x的,直接把他插到链表后部。
刚刚那道题目,采用了一个ListNode endTag来防止重复扫描的问题,即把tail后面,后来插入的结点也给扫描了。
这道题木我换了一种做法。直接做一个计数器记录结点总个数。
然后while循环就用这个做判断。会简单很多。
当然,这道题木,需要判断 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; 必须切断。否则就是一个环,无限循环。
reference:
https://discuss.leetcode.com/topic/7005/very-concise-one-pass-solution/5

特点在于,用了双dummy node
然后想到 odd even linked list 应该也可以这么做
http://www.jianshu.com/p/c4a424042667

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容