zoukankan      html  css  js  c++  java
  • LeetCode() Partition List

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* partition(ListNode* head, int x) {
            if(head == NULL || head->next ==NULL)
                return head;
            ListNode* head1=new ListNode(0);
            ListNode* head2=new ListNode(0);
            ListNode* t1,*t2;
            head1->next=head;
            head2->next=head;
            t1=head1,t2=head2;
            while(head)
            {
    			ListNode* tem;
                if(head->val < x)
                {                
                    tem=head;
                    t1->next=tem;
                    t1=t1->next;             
                }
                else
                {
                    tem=head;
                    t2->next=tem;
                    t2=t2->next;
                }
                head=head->next;
            }
            t2->next=NULL; //必须要加上这句,否则可能是死循环。
            t1->next=head2->next;
            return head1->next;
        }
    };
    

      

  • 相关阅读:
    8.18学习日志
    8.17学习日志
    8.15学习日志
    8.14学习日志
    8.13学习日志
    8.12学习日志
    8.11学习日志
    kindle
    xcode 4 安装cocos2d-x 2.1.4
    GUI之CCControlExtension
  • 原文地址:https://www.cnblogs.com/yanqi110/p/5004850.html
Copyright © 2011-2022 走看看