Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) {
- * val = x;
- * next = null;
- * }
- * }
- */
- public class Solution {
- public ListNode partition(ListNode head, int x) {
- if (head == null) {
- return null;
- }
- ListNode leftDummy = new ListNode(0);
- ListNode rightDummy = new ListNode(0);
- ListNode left = leftDummy, right = rightDummy;
- while (head != null) {
- if (head.val < x) {
- left.next = head;
- left = head;
- } else {
- right.next = head;
- right = head;
- }
- head = head.next;
- }
- right.next = null;
- left.next = rightDummy.next;
- return leftDummy.next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement