File tree 5 files changed +150
-0
lines changed
algorithms/SplitLinkedListInParts
5 files changed +150
-0
lines changed Original file line number Diff line number Diff line change @@ -269,6 +269,7 @@ All solutions will be accepted!
269
269
| 150| [ Evaluate Reverse Polish Notation] ( https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/description/ ) | [ java/py/js] ( ./algorithms/EvaluateReversePolishNotation ) | Medium|
270
270
| 341| [ Flatten Nested List Iterator] ( https://leetcode-cn.com/problems/flatten-nested-list-iterator/description/ ) | [ java/py/js] ( ./algorithms/FlattenNestedListIterator ) | Medium|
271
271
| 856| [ Score Of Parentheses] ( https://leetcode-cn.com/problems/score-of-parentheses/description/ ) | [ java/py/js] ( ./algorithms/ScoreOfParentheses ) | Medium|
272
+ | 725| [ Split Linked List In Parts] ( https://leetcode-cn.com/problems/split-linked-list-in-parts/description/ ) | [ java/py/js] ( ./algorithms/SplitLinkedListInParts ) | Medium|
272
273
273
274
# Database
274
275
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Split Linked List In Parts
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode(int x) { val = x; }
7
+ * }
8
+ */
9
+ class Solution {
10
+ public ListNode [] splitListToParts (ListNode root , int k ) {
11
+ ListNode [] res = new ListNode [k ];
12
+ int length = 0 ;
13
+ ListNode p = root ,
14
+ pre = root ;
15
+
16
+ while (p != null ) {
17
+ p = p .next ;
18
+ length ++;
19
+ }
20
+
21
+ int quotient = length / k ,
22
+ remainder = length % k ;
23
+
24
+ p = root ;
25
+
26
+ int i = 0 ,
27
+ j = 0 ;
28
+
29
+ while (p != null ) {
30
+ if (i ++ == 0 )
31
+ res [j ++] = p ;
32
+
33
+ pre = p ;
34
+ p = p .next ;
35
+
36
+ if (remainder > 0 && i == quotient + 1 ) {
37
+ remainder --;
38
+ pre .next = null ;
39
+ i = 0 ;
40
+ } else if (remainder == 0 && i == quotient ) {
41
+ pre .next = null ;
42
+ i = 0 ;
43
+ }
44
+ }
45
+
46
+ return res ;
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val) {
4
+ * this.val = val;
5
+ * this.next = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } root
10
+ * @param {number } k
11
+ * @return {ListNode[] }
12
+ */
13
+ var splitListToParts = function ( root , k ) {
14
+ let res = [ ] ,
15
+ length = 0 ,
16
+ p = root
17
+
18
+ while ( p ) {
19
+ p = p . next
20
+ length ++
21
+ }
22
+
23
+ let quotient = parseInt ( length / k ) ,
24
+ remainder = length % k
25
+
26
+ p = root
27
+ let pre = p ,
28
+ i = 0
29
+
30
+ while ( p ) {
31
+ if ( i ++ == 0 )
32
+ res . push ( p )
33
+
34
+ pre = p
35
+ p = p . next
36
+
37
+ if ( remainder > 0 && i == quotient + 1 ) {
38
+ remainder --
39
+ pre . next = null
40
+ i = 0
41
+ } else if ( remainder == 0 && i == quotient ) {
42
+ pre . next = null
43
+ i = 0
44
+ }
45
+ }
46
+
47
+ while ( res . length < k )
48
+ res . push ( null )
49
+
50
+ return res
51
+ } ;
Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+
7
+ class Solution (object ):
8
+ def splitListToParts (self , root , k ):
9
+ """
10
+ :type root: ListNode
11
+ :type k: int
12
+ :rtype: List[ListNode]
13
+ """
14
+ res = []
15
+ length = 0
16
+ p = root
17
+
18
+ while p :
19
+ p = p .next
20
+ length += 1
21
+
22
+ quotient = length / k
23
+ remainder = length % k
24
+
25
+ p = root
26
+ pre = p
27
+ i = 0
28
+
29
+ while p :
30
+ if i == 0 :
31
+ res .append (p )
32
+
33
+ i += 1
34
+ pre = p
35
+ p = p .next
36
+
37
+ if remainder > 0 and i == quotient + 1 :
38
+ remainder -= 1
39
+ pre .next = None
40
+ i = 0
41
+ elif remainder == 0 and i == quotient :
42
+ pre .next = None
43
+ i = 0
44
+
45
+ while len (res ) < k :
46
+ res .append (None )
47
+
48
+ return res
You can’t perform that action at this time.
0 commit comments