1// This Question can be solved easily using two standard methods of LinkedList2// 1) addFirst (it adds node in front of the LinkedList)3// 2) addLast (it adds node in end of the LinkedList)45class Solution {67static ListNode oh;8static ListNode ot;9static ListNode th;10static ListNode tt;1112public ListNode reverseEvenLengthGroups(ListNode head) {1314oh = null;15ot = null;16th = null;17tt = null;1819if (head == null || head.next == null) return head;2021int size = length(head);22int idx = 1;23ListNode curr = head;24int group = 1;2526while (curr != null) {27int temp = size - idx + 1;28if ((temp >= group && group % 2 == 0) || (temp < group && temp % 2 == 0)) {29int k = group;30while (k-- > 0 && curr != null) {31ListNode t = curr.next;32curr.next = null;33addFirst(curr);34curr = t;35idx++;36}37} else {38int k = group;39while (k-- > 0 && curr != null) {40ListNode t = curr.next;41curr.next = null;42addLast(curr);43curr = t;44idx++;45}46}4748if (oh == null && ot == null) {49oh = th;50ot = tt;51} else {52ot.next = th;53ot = tt;54}5556th = null;57tt = null;58group++;59}6061return oh;62}6364public int length(ListNode head) {65if (head == null) return 0;66ListNode curr = head;67int k = 0;68while (curr != null) {69k++;70curr = curr.next;71}72return k;73}7475public void addFirst(ListNode head) {76if (tt == null && th == null) {77th = head;78tt = head;79} else {80head.next = th;81th = head;82}83}8485public void addLast(ListNode head) {86if (tt == null && th == null) {87th = head;88tt = head;89} else {90tt.next = head;91tt = head;92}93}94}