-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP7_10.java
More file actions
43 lines (38 loc) · 1.06 KB
/
P7_10.java
File metadata and controls
43 lines (38 loc) · 1.06 KB
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
39
40
41
42
43
public class P7_10 {
public static void main(String[] args) {
}
public ListNode oddEvenList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode current = head;
ListNode insert = current;
ListNode leapNode = current;
while(true)
{
//Leap Current Forward
if(current.next == null)
{
return dummyHead.next;
}
current = current.next;
//At one before next node to swap.
if(current.next == null)
{
return dummyHead.next;
}
leapNode = current.next;
if(leapNode.next == null)
{
current.next = null;
}else
{
current.next = leapNode.next;
}
leapNode.next = insert.next;
insert.next = leapNode;
insert = insert.next;
//After Insertion current has moved up by one. So we dont need to do anything.
}
}
}