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è)局部排序的問題。那么可以想到的一種解法是首先找到第一個(gè)大于或等于給定值的節(jié)點(diǎn),用題目中給的例子來說就是先找到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ú)一無二的二叉搜索樹之二)
- 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語言詳細(xì)分析浮點(diǎn)數(shù)在內(nèi)存中的儲(chǔ)存
我們?cè)谌粘I钪泻途幊讨卸紩?huì)用到小數(shù),比如:3.1415926、29.9、1E10(科學(xué)計(jì)數(shù)法也是浮點(diǎn)型)。在C語言中的浮點(diǎn)型類型有:float,double,long double。那么浮點(diǎn)數(shù)在這些浮點(diǎn)型的內(nèi)存之中又是如何儲(chǔ)存的呢,這就是今天我們要分享的2022-06-06
關(guān)于C語言位運(yùn)算的簡(jiǎn)單示例
這篇文章主要介紹了關(guān)于C語言位運(yùn)算的簡(jiǎn)單示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
淺談帶緩沖I/O 和不帶緩沖I/O的區(qū)別與聯(lián)系
下面小編就為大家?guī)硪黄獪\談帶緩沖I/O 和不帶緩沖I/O的區(qū)別與聯(lián)系。小編覺得挺不錯(cuò)的現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
C++實(shí)現(xiàn)產(chǎn)生隨機(jī)數(shù)和相應(yīng)的猜拳小游戲?qū)嵗a
C++中沒有自帶的random函數(shù),要實(shí)現(xiàn)隨機(jī)數(shù)的生成就需要使用rand()和srand()。下面這篇文章主要給大家介紹了關(guān)于C++實(shí)現(xiàn)產(chǎn)生隨機(jī)數(shù)和相應(yīng)的猜拳小游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09
Qt實(shí)現(xiàn)簡(jiǎn)易計(jì)時(shí)器的示例代碼
計(jì)時(shí)器實(shí)現(xiàn)四個(gè)功能:開始計(jì)時(shí)、停止計(jì)時(shí)、暫停計(jì)時(shí)以及打點(diǎn)。當(dāng)點(diǎn)擊暫停時(shí),開始按鈕和停止按鈕無法點(diǎn)擊。當(dāng)點(diǎn)擊停止時(shí),開始按鈕和暫停按鈕無法點(diǎn)擊,此時(shí)停止按鈕變?yōu)榍辶?。本文將用Qt實(shí)現(xiàn)這樣的一個(gè)計(jì)時(shí)器,需要的可以參考一下2022-06-06
C++標(biāo)準(zhǔn)庫(kù)中sstream與strstream的區(qū)別詳細(xì)解析
以下是對(duì)C++標(biāo)準(zhǔn)庫(kù)中sstream與strstream的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-09-09

