What's wrong with my cpp code?


  • 0
    T
        /**
     * 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) {
            ListNode *left,*right,*temp,*rhead;
            if (head==NULL)
                return head;
            temp=head;
            while (temp){
                if (temp->val<x){
                    if (left){
                        left->next=temp;
                        left=left->next;
                    }
                    else {
                        left=temp;
                        head=temp;
                    }
                }
                else{
                    if (right){
                        right->next=temp;
                        right=right->next;
                    }
                    else {
                        right=temp;
                        rhead=temp;
                    }
                }
                temp=temp->next;
            }
            if (left){
                left->next=rhead;
                right->next=NULL;
            }
            return head;
            
    
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.