-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlternateLL.cpp
57 lines (49 loc) · 1.03 KB
/
AlternateLL.cpp
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
#include<bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node(int data){
this->data = data;
next = nullptr;
}
};
vector<Node*> alternatingSplitList(struct Node* head) {
if(!head) return {};
Node* ptr = head;
vector<Node*> ans;
Node* head1 = NULL, *tail = head1;
while(ptr){
if(!head1){
head1 = new Node(ptr->data);
tail = head1;
}
else {
Node* newNode = new Node(ptr->data);
tail->next = newNode;
tail = tail->next;
}
ptr = ptr->next;
if(ptr) ptr = ptr->next;
}
ans.push_back(head1);
if(head->next) ptr = head->next;
else return ans;
Node* head2 = NULL;
tail = head2;
while(ptr){
if(!head2){
head2 = new Node(ptr->data);
tail = head2;
}
else {
Node* newNode = new Node(ptr->data);
tail->next = newNode;
tail = tail->next;
}
ptr = ptr->next;
if(ptr) ptr = ptr->next;
}
ans.push_back(head2);
return ans;
}