diff --git a/solutions/725.java b/solutions/725.java new file mode 100644 index 0000000..8c30142 --- /dev/null +++ b/solutions/725.java @@ -0,0 +1,60 @@ +/* +Problem Name: Split Linked List in Parts +Problem Link: https://leetcode.com/problems/split-linked-list-in-parts/ +Time: 0ms, 100% +Memory: 39Mb, 75.06% +*/ + + +//-----------------Solution----------------- + +/** + * 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[] splitListToParts(ListNode head, int K) { + if(K==1) + return new ListNode[]{head}; + int size = 0; + ListNode temp = head; + while(temp!=null){ + temp = temp.next; + size++; + } + int d = size/K; + int rem = size%K; + ListNode[] ans = new ListNode[K]; + ListNode pre = null; + int i=0; + while(i0){ + pre = head; + head = head.next; + rem--; + } + pre.next = null; + ans[i++] = start; + } + } + return ans; + + + } +}