Advertisement
sweet1cris

Untitled

Jan 9th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1.  
  2. /**
  3.  * Definition for singly-linked list.
  4.  * public class ListNode {
  5.  *     int val;
  6.  *     ListNode next;
  7.  *     ListNode(int x) {
  8.  *         val = x;
  9.  *         next = null;
  10.  *     }
  11.  * }
  12.  */
  13. public class Solution {
  14.     public ListNode partition(ListNode head, int x) {
  15.         if (head == null) {
  16.             return null;
  17.         }
  18.        
  19.         ListNode leftDummy = new ListNode(0);
  20.         ListNode rightDummy = new ListNode(0);
  21.         ListNode left = leftDummy, right = rightDummy;
  22.        
  23.         while (head != null) {
  24.             if (head.val < x) {
  25.                 left.next = head;
  26.                 left = head;
  27.             } else {
  28.                 right.next = head;
  29.                 right = head;
  30.             }
  31.             head = head.next;
  32.         }
  33.        
  34.         right.next = null;
  35.         left.next = rightDummy.next;
  36.         return leftDummy.next;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement