Created
July 22, 2019 17:59
-
-
Save sogwiz/6905184feedee1da0a0f798a05bde973 to your computer and use it in GitHub Desktop.
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. | |
* 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