-
Notifications
You must be signed in to change notification settings - Fork 6
/
SplitLinkedListInParts.java
50 lines (43 loc) · 1.29 KB
/
SplitLinkedListInParts.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.shivaprasad.january.day21;
//Note: For Linked List problems, Just focus on method code because creating Linked List for every problem is a hectic task,
//and we don't need to worry about it.
//Problem Link: https://leetcode.com/problems/split-linked-list-in-parts/
public class SplitLinkedListInParts {
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; }
}
public ListNode[] splitListToParts(ListNode head, int k) {
ListNode[] res = new ListNode[k];
int totalLength = 0;
ListNode curr = head;
while(curr!=null)
{
totalLength++;
curr = curr.next;
}
int len = totalLength/k;
int leftOver = totalLength%k;
ListNode node = head;
ListNode prev = null;
int i=0;
while(node!=null && i<k)
{
res[i] = node;
for(int j=0;j<len+(leftOver>0?1:0);j++)
{
prev = node;
node = node.next;
}
prev.next = null;
leftOver--;
i++;
}
return res;
//T.C: O(n)
//S.C: O(n)
}
}