-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoddEvenList
More file actions
28 lines (28 loc) · 726 Bytes
/
Copy pathoddEvenList
File metadata and controls
28 lines (28 loc) · 726 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode oddEvenList(ListNode head) {
if(head==null || head.next==null){
return head;
}
ListNode odd=head;
ListNode even=head.next;
ListNode evenHead=even;
while(even!=null && even.next!=null){
odd.next=even.next;
odd=odd.next;
even.next=odd.next;
even=even.next;
}
odd.next=evenHead;
return head;
}
}