Skip to content

Instantly share code, notes, and snippets.

@sogwiz
Created July 22, 2019 17:59
Show Gist options
  • Save sogwiz/6905184feedee1da0a0f798a05bde973 to your computer and use it in GitHub Desktop.
Save sogwiz/6905184feedee1da0a0f798a05bde973 to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class OddEvenLinkedList {
public ListNode oddEvenList(ListNode head) {
if(head == null || head.next == null)return head;
ListNode odd = head;
ListNode evenHead = head.next;
ListNode even = evenHead;
//while (even!=null && even.next != null){
while(even!=null && even.next!=null){
odd.next = odd.next.next;
odd = odd.next;
even.next = even.next.next;
even = even.next;
}
odd.next = evenHead;
return head;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment