-
86. Partition ListAlgorithm/java tip 2021. 3. 3. 17:39
leetcode.com/problems/partition-list/submissions/
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode partition(ListNode head, int x) { ListNode less = new ListNode(0); ListNode greater = new ListNode(0); ListNode lessHead = less; ListNode greaterHead = greater; ListNode cur = head; while(cur != null) { if(cur.val < x) { less.next = cur; less = less.next; } else { greater.next = cur; greater = greater.next; } cur = cur.next; } less.next = greaterHead.next; greater.next = null; return lessHead.next; } }
'Algorithm > java tip' 카테고리의 다른 글
107. Binary Tree Level Order Traversal II (0) 2021.03.03 46. Permutations (0) 2021.03.03 Tip4 (0) 2020.04.19 Tip3(Easy) (0) 2020.04.14 Tip2(easy) (0) 2020.04.13