Created
May 16, 2020 10:00
-
-
Save kenzhemir/ed925ba29262e930895d8d1ff3f21538 to your computer and use it in GitHub Desktop.
Odd Even Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* function ListNode(val, next) { | |
* this.val = (val===undefined ? 0 : val) | |
* this.next = (next===undefined ? null : next) | |
* } | |
*/ | |
/** | |
* @param {ListNode} head | |
* @return {ListNode} | |
*/ | |
var oddEvenList = function(head) { | |
if (head == null || head.next == null || head.next.next == null) { | |
return head; | |
} | |
let odd = head; | |
let evenHead = head.next; | |
let even = evenHead; | |
while(odd.next!=null && even.next!=null) { | |
odd.next = even.next | |
odd = odd.next | |
even.next = odd.next | |
even = even.next | |
} | |
odd.next = evenHead | |
return head | |
}; | |
//https://leetcode.com/problems/odd-even-linked-list/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment