86. Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

** 解題思路**
用兩個(gè)queue 來分別存放 val < x 和 val >=x 的node馋记, 然后把這兩個(gè)node 連起來即可梯醒。
注意,要把第二個(gè)queue的尾巴設(shè)置為null畜隶,避免循環(huán)。
the basic idea is to maintain two queues, the first one stores all nodes with val less than x, and the second queue stores all the rest nodes. Then concat these two queues.
Remember to set the tail of second queue a null next, otherwise you will get TLE.

/* the basic idea is to maintain two queues, the first one stores all nodes with val less than x, and the second queue stores all the rest nodes. Then concat these two queues.
 Remember to set the tail of second queue a null next, otherwise you will get TLE.
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode dummy1 = new ListNode(0);  //dummy heads of the 1st and 2nd queues
        ListNode dummy2 = new ListNode(0);  //current tails of the two queues;
        ListNode curr1 = dummy1;
        ListNode curr2 = dummy2;
        
        while (head != null) {
            if (head.val < x) {
                curr1.next = head;
                curr1 = head;
            } else {
                curr2.next = head;
                curr2 = head;
            }
            head = head.next;
        }
        curr2.next = null;  //important! avoid cycle in linked list. otherwise u will get TLE.
        curr1.next = dummy2.next;
        return dummy1.next;
    }
    // 失敗的
    public ListNode partitionFailed(ListNode head, int x) {
      ListNode dummy = new ListNode(0);
      dummy.next = head;
      ListNode pre = dummy;
      while (pre.next != null && pre.next.next != null) {
          ListNode first = pre.next;
          ListNode second = pre.next.next;
          if (first.val >= x && second.val < x) {
              first.next = second.next;
              pre.next = second;
              pre.next.next = first;
              pre = pre.next.next;
          }
          pre = pre.next.next;
      }
      return dummy.next;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末猫胁,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子届惋,更是在濱河造成了極大的恐慌,老刑警劉巖脑豹,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瘩欺,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡筑悴,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門砚婆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人坷虑,你說我怎么就攤上這事埂奈。” “怎么了账磺?”我有些...
    開封第一講書人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵垮抗,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我液茎,道長(zhǎng)辞嗡,這世上最難降的妖魔是什么捆等? 我笑而不...
    開封第一講書人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任楚里,我火速辦了婚禮,結(jié)果婚禮上班缎,老公的妹妹穿的比我還像新娘她渴。我一直安慰自己,他們只是感情好沉唠,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開白布苛败。 她就那樣靜靜地躺著罢屈,像睡著了一般。 火紅的嫁衣襯著肌膚如雪锄贷。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評(píng)論 1 302
  • 那天柔昼,我揣著相機(jī)與錄音炎辨,去河邊找鬼。 笑死激率,一個(gè)胖子當(dāng)著我的面吹牛勿决,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播嘉冒,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼讳推,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼玩般!你這毒婦竟也來了坏为?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤洒忧,失蹤者是張志新(化名)和其女友劉穎够颠,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體履磨,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡巷送,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了惩系。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片如筛。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖杨刨,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情芥颈,我是刑警寧澤爬坑,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布涂臣,位于F島的核電站,受9級(jí)特大地震影響署辉,放射性物質(zhì)發(fā)生泄漏岩四。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一材鹦、第九天 我趴在偏房一處隱蔽的房頂上張望侠姑。 院中可真熱鬧,春花似錦箩做、人聲如沸莽红。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽安吁。三九已至,卻和暖如春燃辖,著一層夾襖步出監(jiān)牢的瞬間鬼店,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來泰國打工黔龟, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留妇智,地道東北人滥玷。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像巍棱,于是被迫代替她去往敵國和親惑畴。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

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