C# Solution


  • 0
        public ListNode Partition(ListNode head, int x) {
            if (head == null)
                return head;
            
            ListNode lessDummy = new ListNode(-1),
                     lessCurrent = lessDummy,
                     greaterDummy = new ListNode(-1),
                     greaterCurrent = greaterDummy;
            
            while (head != null)
            {
                if (head.val < x)
                {
                    lessCurrent.next = head;
                    head = head.next;
                    lessCurrent = lessCurrent.next;
                    lessCurrent.next = null;
                }
                else
                {
                    greaterCurrent.next = head;
                    head = head.next;
                    greaterCurrent = greaterCurrent.next;
                    greaterCurrent.next = null;
                }
            }
    
            lessCurrent.next = greaterDummy.next;
            
            return lessDummy.next;
        }

Log in to reply
 

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