-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem25.c
More file actions
119 lines (98 loc) Β· 2.58 KB
/
problem25.c
File metadata and controls
119 lines (98 loc) Β· 2.58 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 10000
// Linked list node
struct ListNode {
int val;
struct ListNode* next;
};
// Create a new node
struct ListNode* createNode(int val) {
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = val;
newNode->next = NULL;
return newNode;
}
// Count total nodes
int countNodes(struct ListNode* head) {
int count = 0;
while (head) {
count++;
head = head->next;
}
return count;
}
// Split the list
struct ListNode** splitListToParts(struct ListNode* head, int k) {
int totalNodes = countNodes(head);
int baseSize = totalNodes / k;
int extra = totalNodes % k;
struct ListNode** result = (struct ListNode**)malloc(sizeof(struct ListNode*) * k);
for (int i = 0; i < k; i++) {
result[i] = head;
int partSize = baseSize + (i < extra ? 1 : 0);
for (int j = 1; j < partSize; j++) {
if (head) head = head->next;
}
if (head) {
struct ListNode* temp = head->next;
head->next = NULL;
head = temp;
}
}
return result;
}
// Print linked list
void printList(struct ListNode* head) {
if (!head) {
printf("[]\n");
return;
}
printf("[");
while (head) {
printf("%d", head->val);
if (head->next) printf(", ");
head = head->next;
}
printf("]\n");
}
int main() {
char buffer[MAX_LEN];
int k;
printf("Enter linked list elements (space separated), then press Enter twice:\n");
// Read first line input
fgets(buffer, MAX_LEN, stdin);
if (buffer[0] == '\n') {
printf("Empty input.\n");
return 0;
}
// Remove trailing newline
buffer[strcspn(buffer, "\n")] = '\0';
// Tokenize input into linked list
struct ListNode* head = NULL, *tail = NULL;
char* token = strtok(buffer, " ");
while (token != NULL) {
int val = atoi(token);
struct ListNode* newNode = createNode(val);
if (!head) head = tail = newNode;
else {
tail->next = newNode;
tail = newNode;
}
token = strtok(NULL, " ");
}
// Wait for an empty Enter
fgets(buffer, MAX_LEN, stdin);
// Read k
printf("Enter number of parts (k): ");
scanf("%d", &k);
// Split list and print parts
struct ListNode** parts = splitListToParts(head, k);
printf("Output Parts:\n");
for (int i = 0; i < k; i++) {
printList(parts[i]);
}
free(parts);
return 0;
}