Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split Linked List in Part - Java #293

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Java/SplitLinkedListinParts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class SplitLinkedListToParts {
static 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 static ListNode[] splitListToParts(ListNode head, int k) {
ListNode temp = head;
ListNode pre = head;
ListNode[] arr = new ListNode[k];
int count = 0;
while (temp != null) {
count++;
temp = temp.next;
}
temp = head;
int i = 0;
int mid = count / k;
int extra = count % k;
while (temp != null && i < k) {
arr[i] = temp;
int part = mid + (extra > 0 ? 1 : 0);
for (int j = 0; j < part - 1; j++) {
temp = temp.next;
}
pre = temp.next;
temp.next = null;
temp = pre;
i++;
extra--;
}

return arr;

}
public static void main(String[] args) {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
int k = 5;
ListNode[]result = splitListToParts(head,k);
for (int i = 0; i < result.length; i++) {
System.out.print("Part " + (i + 1) + ": ");
ListNode node = result[i];
if(node == null){
System.out.println("null");
}else{
while (node != null) {
System.out.print(node.val + " ");
node = node.next;
}
}
System.out.println();
}

}
}