Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cc22876

Browse files
added 143.Reorder_List.cpp using cpp , 95.98% faster (#183)
* Create 143.Reorder_List.cpp * readme
1 parent 7da3fcb commit cc22876

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

‎C++/143.Reorder_List.cpp‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* medium difficulty */
2+
3+
class Solution {
4+
public:
5+
void reorderList(ListNode* head) {
6+
7+
//edge cases
8+
if ((!head) || (!head->next) || (!head->next->next)) return;
9+
10+
int l=0; //to calculate the length
11+
ListNode* curr=head;
12+
while(curr){
13+
l++;
14+
curr=curr->next;
15+
}
16+
17+
//stack to store the second half of the elements of the ll
18+
stack<ListNode*> s;
19+
curr=head;
20+
21+
//iterating till the end of the first half
22+
int m=(l+1)/2;
23+
while(m>=1){
24+
curr=curr->next;
25+
m--;
26+
}
27+
28+
//pushing the second half of the elements in the stack
29+
while(curr){
30+
s.push(curr);
31+
curr=curr->next;
32+
}
33+
34+
//attaching the elements from the top of the stack to the first half of the elements
35+
curr=head;
36+
while(curr&&!s.empty()){
37+
ListNode* temp=s.top();
38+
s.pop();
39+
temp->next=curr->next;
40+
curr->next=temp;
41+
curr=curr->next->next;
42+
}
43+
curr->next=NULL;
44+
}
45+
};

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
186186
| 142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Java](./Java/linked-list-cycle-ii.java) <br> [C++](./C++/Linked-List-Cycle-II.cpp) | _O(n)_ | _O(1)_ | Medium | Slow-Fast Pointers | |
187187
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [C++](./C++/LRU-Cache.cpp) <br> [Python](./Python/LRUCache.py) | _O(1)_ | _O(k)_ | Medium | Hash Map | |
188188
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [Java](./Java/intersection-of-two-linked-lists.java) | _O(n)_ | _O(1)_ | Easy | Two Pointers | [Tutorial](https://youtu.be/uozGB0-gbvI) |
189-
| 186 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Java](./Java/middle-of-the-linked-list.java) | _O(n)_ | _O(1)_ | Easy | Two pointers | |
189+
| 186 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Java](./Java/middle-of-the-linked-list.java) | _O(n)_ | _O(1)_ | Easy | Two pointers |
190+
| 143 | [Reorder List](https://leetcode.com/problems/reorder-list/) | [C++](./C++/143.Reorder_List.cpp) | _O(n)_ | _O(n)_ | Medium | Iteration and Stack |
191+
|
190192

191193
<br/>
192194
<div align="right">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /