File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed
algorithms/cpp/oddEvenLinkedList Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 8
8
9
9
| # | Title | Solution | Difficulty |
10
10
|---| ----- | -------- | ---------- |
11
+ |328|[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/) | [C++](./algorithms/cpp/oddEvenLinkedList/OddEvenLinkedList.cpp)|Easy|
11
12
|327|[Count of Range Sum](https://leetcode.com/problems/count-of-range-sum/) | [C++](./algorithms/cpp/countOfRangeSum/CountOfRangeSum.cpp)|Hard|
12
13
|326|[Power of Three](https://leetcode.com/problems/power-of-three/) | [C++](./algorithms/cpp/powerOfThree/PowerOfThree.cpp)|Easy|
13
14
|322|[Coin Change](https://leetcode.com/problems/coin-change/) | [C++](./algorithms/cpp/coinChange/coinChange.cpp)|Medium|
Original file line number Diff line number Diff line change
1
+ // Source : https://leetcode.com/problems/odd-even-linked-list/
2
+ // Author : Hao Chen
3
+ // Date : 2016-01-16
4
+
5
+ /***************************************************************************************
6
+ *
7
+ * Given a singly linked list, group all odd nodes together followed by the even nodes.
8
+ * Please note here we are talking about the node number and not the value in the nodes.
9
+ *
10
+ * You should try to do it in place. The program should run in O(1) space complexity
11
+ * and O(nodes) time complexity.
12
+ *
13
+ * Example:
14
+ * Given 1->2->3->4->5->NULL,
15
+ * return 1->3->5->2->4->NULL.
16
+ *
17
+ * Note:
18
+ * The relative order inside both the even and odd groups should remain as it was in
19
+ * the input.
20
+ * The first node is considered odd, the second node even and so on ...
21
+ *
22
+ * Credits:Special thanks to @aadarshjajodia for adding this problem and creating all
23
+ * test cases.
24
+ ***************************************************************************************/
25
+
26
+ /**
27
+ * Definition for singly-linked list.
28
+ * struct ListNode {
29
+ * int val;
30
+ * ListNode *next;
31
+ * ListNode(int x) : val(x), next(NULL) {}
32
+ * };
33
+ */
34
+ class Solution {
35
+ public:
36
+ ListNode* oddEvenList(ListNode* head) {
37
+ if (!head) return head;
38
+ ListNode* pOdd = head;
39
+ ListNode* p = head->next;
40
+ ListNode* pNext = NULL;
41
+ while(p && (pNext=p->next)) {
42
+
43
+ p->next = pNext->next;
44
+ pNext->next = pOdd->next;
45
+ pOdd->next = pNext;
46
+
47
+ p = p->next;
48
+ pOdd = pOdd->next;
49
+
50
+ }
51
+ return head;
52
+ }
53
+ };
2C71
You can’t perform that action at this time.
0 commit comments