Partition List java solution


  • 0
    V

    public class Solution {

    public ListNode partition(ListNode head, int x) {
    
        if(head == null || head.next == null){
            return head;
        }
        
      ListNode dummy = new ListNode(0);
      ListNode result = dummy;
      
      ListNode node1 = head;
      while(node1 != null){
          if(node1.val < x){
              dummy.next = new ListNode(node1.val);
              dummy = dummy.next;
          }
          node1 = node1.next;
      }
      
      ListNode node2 = head;
      while(node2 != null){
          if(node2.val >= x){
              dummy.next = new ListNode(node2.val);
              dummy = dummy.next;
          }
          node2 = node2.next;
      }
      
     return result.next;  
    }
    

    }


Log in to reply
 

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