C++實(shí)現(xiàn)LeetCode(86.劃分鏈表)
[LeetCode] 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.
這道題要求我們劃分鏈表,把所有小于給定值的節(jié)點(diǎn)都移到前面,大于該值的節(jié)點(diǎn)順序不變,相當(dāng)于一個(gè)局部排序的問(wèn)題。那么可以想到的一種解法是首先找到第一個(gè)大于或等于給定值的節(jié)點(diǎn),用題目中給的例子來(lái)說(shuō)就是先找到4,然后再找小于3的值,每找到一個(gè)就將其取出置于4之前即可,代碼如下:
解法一
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
ListNode *dummy = new ListNode(-1);
dummy->next = head;
ListNode *pre = dummy, *cur = head;;
while (pre->next && pre->next->val < x) pre = pre->next;
cur = pre;
while (cur->next) {
if (cur->next->val < x) {
ListNode *tmp = cur->next;
cur->next = tmp->next;
tmp->next = pre->next;
pre->next = tmp;
pre = pre->next;
} else {
cur = cur->next;
}
}
return dummy->next;
}
};
這種解法的鏈表變化順序?yàn)椋?/p>
1 -> 4 -> 3 -> 2 -> 5 -> 2
1 -> 2 -> 4 -> 3 -> 5 -> 2
1 -> 2 -> 2 -> 4 -> 3 -> 5
此題還有一種解法,就是將所有小于給定值的節(jié)點(diǎn)取出組成一個(gè)新的鏈表,此時(shí)原鏈表中剩余的節(jié)點(diǎn)的值都大于或等于給定值,只要將原鏈表直接接在新鏈表后即可,代碼如下:
解法二
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if (!head) return head;
ListNode *dummy = new ListNode(-1);
ListNode *newDummy = new ListNode(-1);
dummy->next = head;
ListNode *cur = dummy, *p = newDummy;
while (cur->next) {
if (cur->next->val < x) {
p->next = cur->next;
p = p->next;
cur->next = cur->next->next;
p->next = NULL;
} else {
cur = cur->next;
}
}
p->next = dummy->next;
return newDummy->next;
}
};
此種解法鏈表變化順序?yàn)椋?/p>
Original: 1 -> 4 -> 3 -> 2 -> 5 -> 2
New:
Original: 4 -> 3 -> 2 -> 5 -> 2
New: 1
Original: 4 -> 3 -> 5 -> 2
New: 1 -> 2
Original: 4 -> 3 -> 5
New: 1 -> 2 -> 2
Original:
New: 1 -> 2 -> 2 -> 4 -> 3 -> 5
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(86.劃分鏈表)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)劃分鏈表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(95.獨(dú)一無(wú)二的二叉搜索樹(shù)之二)
- C++實(shí)現(xiàn)LeetCode(93.復(fù)原IP地址)
- C++實(shí)現(xiàn)LeetCode(91.解碼方法)
- C++實(shí)現(xiàn)LeetCode(137.單獨(dú)的數(shù)字之二)
- C++實(shí)現(xiàn)LeetCode(136.單獨(dú)的數(shù)字)
- C++實(shí)現(xiàn)LeetCode(187.求重復(fù)的DNA序列)
- C++實(shí)現(xiàn)LeetCode(89.格雷碼)
- C++實(shí)現(xiàn)LeetCode(87.攪亂字符串)
- C++實(shí)現(xiàn)LeetCode(139.拆分詞句)
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)BMP轉(zhuǎn)換JPG的方法
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)BMP轉(zhuǎn)換JPG的方法,涉及C#圖片格式轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
OpenCV計(jì)算輪廓長(zhǎng)度/周長(zhǎng)和面積
這篇文章主要為大家詳細(xì)介紹了OpenCV計(jì)算輪廓長(zhǎng)度/周長(zhǎng)和面積,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
kernel利用pt?regs劫持seq?operations的遷移過(guò)程詳解
這篇文章主要為大家介紹了kernel利用pt_regs劫持seq_operations進(jìn)行遷移的過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
C++學(xué)習(xí)筆記之類(lèi)與對(duì)象詳解
這篇文章主要為大家介紹了C++類(lèi)與對(duì)象,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12
C++實(shí)現(xiàn)小型復(fù)數(shù)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)小型復(fù)數(shù)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
C++實(shí)現(xiàn)LeetCode(206.倒置鏈表)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(206.倒置鏈表),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

