Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenzhemir/ed925ba29262e930895d8d1ff3f21538 to your computer and use it in GitHub Desktop.
Save kenzhemir/ed925ba29262e930895d8d1ff3f21538 to your computer and use it in GitHub Desktop.
Odd Even Linked List
/**
* 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