码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode-86-Longest Palindromic Characters

时间:2019-02-01 14:19:34      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:思路   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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!