-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0328-OddEvenLinkedList.cs
38 lines (36 loc) · 1.07 KB
/
0328-OddEvenLinkedList.cs
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
29
30
31
32
33
34
35
36
37
38
//-----------------------------------------------------------------------------
// Runtime: 92ms
// Memory Usage: 25.6 MB
// Link: https://leetcode.com/submissions/detail/340551981/
//-----------------------------------------------------------------------------
namespace LeetCode
{
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class _0328_OddEvenLinkedList
{
public ListNode OddEvenList(ListNode head)
{
if (head == null) return null;
ListNode odd = head, even = head.next, 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;
}
}
}