File tree 5 files changed +116
-0
lines changed
algorithms/OddEvenLinkedList
5 files changed +116
-0
lines changed Original file line number Diff line number Diff line change @@ -239,6 +239,7 @@ All solutions will be accepted!
239
239
| 208| [ Implement Trie Prefix Tree] ( https://leetcode-cn.com/problems/implement-trie-prefix-tree/description/ ) | [ java/py/js] ( ./algorithms/ImplementTriePrefixTree ) | Medium|
240
240
| 114| [ Flatten Binary Tree To Linked List] ( https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/description/ ) | [ java/py/js] ( ./algorithms/FlattenBinaryTreeToLinkedList ) | Medium|
241
241
| 284| [ Peeking Iterator] ( https://leetcode-cn.com/problems/peeking-iterator/description/ ) | [ java/py] ( ./algorithms/PeekingIterator ) | Medium|
242
+ | 328| [ Odd Even Linked List] ( https://leetcode-cn.com/problems/odd-even-linked-list/description/ ) | [ java/py/js] ( ./algorithms/OddEvenLinkedList ) | Medium|
242
243
243
244
# Database
244
245
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Odd Even Linked List
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode(int x) { val = x; }
7
+ * }
8
+ */
9
+ class Solution {
10
+ public ListNode oddEvenList (ListNode head ) {
11
+ if (head == null ) return null ;
12
+
13
+ int length = 0 ;
14
+ ListNode tail = head ,
15
+ node = head ;
16
+
17
+ while (node != null ) {
18
+ length ++;
19
+ tail = node ;
20
+ node = node .next ;
21
+ }
22
+
23
+ ListNode pre = head ;
24
+ int i = 1 ;
25
+ node = pre .next ;
26
+
27
+ while (i < length && node .next != null ) {
28
+ pre .next = node .next ;
29
+ node .next = null ;
30
+ tail .next = node ;
31
+ tail = tail .next ;
32
+ pre = pre .next ;
33
+ node = pre .next ;
34
+ i += 2 ;
35
+ }
36
+
37
+ return head ;
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val) {
4
+ * this.val = val;
5
+ * this.next = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } head
10
+ * @return {ListNode }
11
+ */
12
+ var oddEvenList = function ( head ) {
13
+ if ( ! head ) return null
14
+
15
+ let length = 0 ,
16
+ tail = head ,
17
+ node = head
18
+
19
+ while ( node ) {
20
+ length ++
21
+ tail = node
22
+ node = node . next
23
+ }
24
+
25
+ let pre = head ,
26
+ i = 1
27
+ node = pre . next
28
+
29
+ while ( i < length && node . next ) {
30
+ pre . next = node . next
31
+ node . next = null
32
+ tail . next = node
33
+ tail = tail . next
34
+ pre = pre . next
35
+ node = pre . next
36
+ i += 2
37
+ }
38
+
39
+ return head
40
+ } ;
Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+
7
+ class Solution (object ):
8
+ def oddEvenList (self , head ):
9
+ """
10
+ :type head: ListNode
11
+ :rtype: ListNode
12
+ """
13
+ if not head :
14
+ return None
15
+
16
+ length = 0
17
+ tail = node = head
18
+ while node :
19
+ length += 1
20
+ tail , node = node , node .next
21
+
22
+ pre = head
23
+ node = pre .next
24
+ i = 1
25
+ while i < length and node .next :
26
+ pre .next = node .next
27
+ node .next = None
28
+ tail .next = node
29
+ tail = tail .next
30
+ pre = pre .next
31
+ node = pre .next
32
+ i += 2
33
+ return head
34
+
You can’t perform that action at this time.
0 commit comments