1+ /*
2+
3+ Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes
4+ with even indices, and return the reordered list.
5+
6+ The first node is considered odd, and the second node is even, and so on.
7+
8+ Note that the relative order inside both the even and odd groups should remain as it was in the input.
9+
10+ You must solve the problem in O(1) extra space complexity and O(n) time complexity.
11+
12+ Input: head = [1,2,3,4,5]
13+ Output: [1,3,5,2,4]
14+
15+ Constraints:
16+ n == number of nodes in the linked list
17+ 0 <= n <= 104
18+ -106 <= Node.val <= 106
19+
20+ */
21+
22+ /*
23+ Time Complexity: O(N), where N is the size of the array
24+ Space Complexity: O(1), Constant Space
25+ */
26+
27+ /*
28+
29+ Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes
30+ with even indices, and return the reordered list.
31+
32+ The first node is considered odd, and the second node is even, and so on.
33+
34+ Note that the relative order inside both the even and odd groups should remain as it was in the input.
35+
36+ You must solve the problem in O(1) extra space complexity and O(n) time complexity.
37+
38+ Input: head = [1,2,3,4,5]
39+ Output: [1,3,5,2,4]
40+
41+ Constraints:
42+ n == number of nodes in the linked list
43+ 0 <= n <= 104
44+ -106 <= Node.val <= 106
45+
46+ */
47+
48+ /*
49+ Time Complexity: O(N), where N is the size of the array
50+ Space Complexity: O(1), Constant Space
51+ */
52+
53+ #include < bits/stdc++.h>
54+ using namespace std ;
55+
56+ struct ListNode // Linked list node
57+ {
58+ int val;
59+ ListNode *next;
60+ ListNode (int x)
61+ {
62+ val = x;
63+ next = NULL ;
64+ }
65+ };
66+
67+ ListNode *oddEvenList (ListNode *head)
68+ {
69+ int len = 1 ;// variable to store length of linked List
70+ ListNode *end = head;
71+ if (head == NULL )
72+ return NULL ;
73+ while (end->next != NULL ) // calculation of length
74+ {
75+ end = end->next ;
76+ len++;
77+ }
78+ if (len <= 2 )
79+ return head;
80+ ListNode *Nend = end; // pointer to point to the new end of the list
81+ ListNode *temp = head;
82+ while (temp != end)
83+ {
84+ ListNode *t = temp->next ;
85+ temp->next = t->next ; // breaking the link
86+ temp = temp->next ;
87+ t->next = NULL ;
88+ Nend->next = t; // adding the node to the end
89+ Nend = Nend->next ; // updating end
90+ if (t == end)
91+ break ;
92+ }
93+ return head;
94+ }
95+ void printList (ListNode *head) // function to print the list
96+ {
97+ ListNode *temp = head;
98+ while (temp != NULL )
99+ {
100+ cout<<temp->val <<" " ;
101+ temp = temp->next ;
102+ }
103+ cout<<" \n " ;
104+ }
105+ int main () // driver function
106+ {
107+ int data = 1 ; // adding the elements to the list
108+ ListNode *head = new ListNode (data);
109+ ListNode *tail = head;
110+ for (int i = 2 ; i<=5 ; i++)
111+ {
112+ tail->next = new ListNode (i);
113+ tail = tail->next ;
114+ }
115+
116+ head = oddEvenList (head);
117+ printList (head);
118+
119+ return 0 ;
120+ }
0 commit comments