Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. //100
  2. class Solution {
  3.     public ListNode partition(ListNode head, int x) {
  4.         if(head == null || head.next == null) return head;
  5.        
  6.         ListNode first_head = new ListNode(-1);
  7.         ListNode first = first_head;
  8.         ListNode second_head = new ListNode(-1);
  9.         ListNode second = second_head;
  10.        
  11.         while(head != null){
  12.             if(head.val >= x){
  13.                 second.next = head;
  14.                 second = second.next;
  15.             } else {
  16.                 first.next = head;
  17.                 first = first.next;
  18.             }
  19.             head = head.next;
  20.         }
  21.        
  22.         second.next = null;
  23.         first.next = second_head.next;
  24.         first_head = first_head.next;
  25.        
  26.         return first_head;
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement