标签:思路 hat ali return nod etc char origin cte
算法描述:
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.
Example:
Input: head = 1->4->3->2->5->2, x = 3 Output: 1->2->2->4->3->5
解题思路:链表题,画图就可以解出来。用两个指针分别指向不同的段,然后将两个指针合并起来,注意尾指针清空。
ListNode* partition(ListNode* head, int x) { if(head == nullptr) return head; ListNode* LowHead = new ListNode(-1); ListNode* HighHead = new ListNode(-1); ListNode* low=LowHead; ListNode* high=HighHead; while(head!=nullptr){ if(head->val <x){ low->next = head; low=low->next; }else{ high->next = head; high=high->next; } head=head->next; } low->next=HighHead->next; high->next = nullptr; return LowHead->next; }
LeetCode-86-Longest Palindromic Characters
标签:思路 hat ali return nod etc char origin cte
原文地址:https://www.cnblogs.com/nobodywang/p/10345514.html