重口味手语书# Joke - 肚皮舞运动
a*e
1 楼
这些题看着容易,老是写不对或者代码很冗长,有大牛们指点一下如何提高么?
呜呜,不知道哪年哪月才能刷完一遍,有同学互勉一下么?
比如
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.
我的
Runtime Error
Last executed input:
{1}, 0
ListNode *partition(ListNode *head, int x) {
if(head==NULL)
return head;
ListNode *dummy = new ListNode(0);
dummy->next = head;
ListNode *cur = head->next;
ListNode *curHead = head;
ListNode *prev = head;
bool flag = false;
while(curHead!=NULL||cur!=NULL||prev!=NULL){
if ((cur->valval>x)){
cur = cur?cur->next:NULL;
curHead = curHead->next;
prev = prev->next;
}
else if (flag==false&&cur->val>=x){
flag=true;
prev = prev->next;
cur = cur?cur->next:NULL;
}
else if (flag==true&&cur->val prev->next = cur->next;
cur->next = curHead->next;
curHead->next = cur;
curHead = cur;
}
else{
cur = cur?cur->next:NULL;
curHead = curHead->next;
prev = prev->next;
}
}
return dummy->next;
}
看到别的答案,
ListNode* partition(ListNode* head, int x) {
ListNode left_dummy(-1); //
ListNode right_dummy(-1); //
auto left_cur = &left_dummy;
auto right_cur = &right_dummy;
for (ListNode *cur = head; cur; cur = cur->next) {
if (cur->val < x) {
left_cur->next = cur;
left_cur = cur;
} else {
right_cur->next = cur;
right_cur = cur;
}
}
left_cur->next = right_dummy.next;
right_cur->next = nullptr;
return left_dummy.next;
呜呜,不知道哪年哪月才能刷完一遍,有同学互勉一下么?
比如
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.
我的
Runtime Error
Last executed input:
{1}, 0
ListNode *partition(ListNode *head, int x) {
if(head==NULL)
return head;
ListNode *dummy = new ListNode(0);
dummy->next = head;
ListNode *cur = head->next;
ListNode *curHead = head;
ListNode *prev = head;
bool flag = false;
while(curHead!=NULL||cur!=NULL||prev!=NULL){
if ((cur->val
cur = cur?cur->next:NULL;
curHead = curHead->next;
prev = prev->next;
}
else if (flag==false&&cur->val>=x){
flag=true;
prev = prev->next;
cur = cur?cur->next:NULL;
}
else if (flag==true&&cur->val
cur->next = curHead->next;
curHead->next = cur;
curHead = cur;
}
else{
cur = cur?cur->next:NULL;
curHead = curHead->next;
prev = prev->next;
}
}
return dummy->next;
}
看到别的答案,
ListNode* partition(ListNode* head, int x) {
ListNode left_dummy(-1); //
ListNode right_dummy(-1); //
auto left_cur = &left_dummy;
auto right_cur = &right_dummy;
for (ListNode *cur = head; cur; cur = cur->next) {
if (cur->val < x) {
left_cur->next = cur;
left_cur = cur;
} else {
right_cur->next = cur;
right_cur = cur;
}
}
left_cur->next = right_dummy.next;
right_cur->next = nullptr;
return left_dummy.next;