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:
這道題目類似于快排鏈表的一個小操作,換結(jié)點。
但是這比插入排序鏈表要爽多了。。。
因為我這里總是在尾部插入,可以省掉許多考慮。。
早晨起來覺得拖著也是拖著,就把一家公司的網(wǎng)上筆試做了。
20道題目,35分鐘。感覺很奇怪。。。不難,但又不簡單,或者說,就是智商題吧。。
不知道做的怎么樣。也沒底。感覺不差,也不好。
9.30微軟面試,我得好好準備下了。
想把之前寫的文章全部重看一遍。然后基礎(chǔ)的排序什么的,必須搞清楚。
今天一定要把CG搞定!!!
感覺寫的還是很復(fù)雜,實在不能理解,為什么要統(tǒng)計數(shù)字。是怕把tail后面的也重復(fù)遍歷進去嗎?
我的簡化代碼如下:
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為止,結(jié)束循環(huán)。然后單獨處理tail就可以了。
不寫了。明天面試。回去復(fù)習(xí)下簡歷,問答。
好運!!!
**
總結(jié): 快排鏈表
**
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;
}
}
這次寫的感覺比以前簡單一些。。
然后我的思路,和剛剛有道題目的第二種解法一樣。
- Odd Even Linked List
http://www.lxweimin.com/p/c4a424042667
這道題木,第二種解法,就是掃描鏈表。碰到偶數(shù)的,就把他直接插到鏈表后部。
這道題木也一樣。碰到 >= x的,直接把他插到鏈表后部。
剛剛那道題目,采用了一個ListNode endTag來防止重復(fù)掃描的問題,即把tail后面,后來插入的結(jié)點也給掃描了。
這道題木我換了一種做法。直接做一個計數(shù)器記錄結(jié)點總個數(shù)。
然后while循環(huán)就用這個做判斷。會簡單很多。
當(dāng)然,這道題木,需要判斷 curr == tail
如果等于的話,我的當(dāng)前代碼是不能把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);
}
}
我的做法挺復(fù)雜,也沒做出來。最后估計可以debug出來,但corner case 太多。
這個做法比較簡潔易懂,關(guān)注點在于:
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 應(yīng)該也可以這么做
http://www.lxweimin.com/p/c4a424042667
Anyway, Good luck, Richardo! --- 08/17/2016