Given a singly linked list containing N nodes. Modify the Linked list as follows:
1. Modify the value of the first half nodes such that 1st node's new value is equal to the value of the last node minus the first node's current value, 2nd node's new value is equal to the second last nodes value minus 2nd nodes current value, likewise for first half nodes.
2. Replace the second half of nodes with the initial values of the first half nodes(values before modifying the nodes).
3. If N is odd then the value of the middle node remains unchanged.
Example 1:
Input:
N = 5
linked list = 10 -> 4 -> 5 -> 3 -> 6
Output:
-4 -1 5 4 10
Explanation:
For first half nodes modified list will be:
-4 -> -1 -> 5 -> 3 -> 6
For second half nodes modified list will be:
-4 -> -1 -> 5 -> 4 -> 10
Example 2:
Input:
N = 6
linked list = 2 -> 9 -> 8 -> 12 -> 7 -> 10
Output:
8 -2 4 8 9 2
Explanation:
After modifying the linked list as required,
we have a new linked list containing the elements as
8 -> -2 -> 4 -> 8 -> 9 -> 2.
YourTask:
The task is to complete the function modifyTheList() which should modify the list as required.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= T <= 200
1 <= N <= 100

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Data structures

Starter code: #User function Template for python3



"""

# Node Class



class node:

    def __init__(self, val):

        self.data = val

        self.next = None



"""



class Solution:

    #Function to reverse a linked list.

    def modifyTheList(self, head):

        # Code here

Solutions:
class Solution:

	def modifyTheList(self, head):
		if not head or not head.next:
			return head
		length = 0
		current = head
		middle = head
		while current and current.next:
			length += 2
			middle = middle.next
			current = current.next.next
		if current:
			length += 1
		prev = None
		current = middle
		while current:
			next_node = current.next
			current.next = prev
			prev = current
			current = next_node
		first_half = head
		second_half = prev
		tpe = second_half
		for _ in range(length // 2):
			tmp = first_half.data
			first_half.data = second_half.data - first_half.data
			second_half.data = tmp
			first_half = first_half.next
			second_half = second_half.next
		second_half = tpe
		prev = None
		current = second_half
		i = 0
		while current and i <= length // 2:
			next_node = current.next
			current.next = prev
			prev = current
			current = next_node
			i += 1
		return head


class Solution:

	def count(self, p):
		cnt = 0
		while p:
			cnt += 1
			p = p.next
		return cnt

	def reverse(self, p):
		prev = None
		while p:
			next = p.next
			p.next = prev
			prev = p
			p = next
		return prev

	def solve(self, p, q):
		while q:
			(p.data, q.data) = (q.data - p.data, p.data)
			p = p.next
			q = q.next

	def modifyTheList(self, head):
		n = self.count(head)
		k = (n + 1) // 2
		p = head
		prev = None
		while k > 0:
			prev = p
			p = p.next
			k -= 1
		p = self.reverse(p)
		self.solve(head, p)
		p = self.reverse(p)
		prev.next = p
		return head


class Solution:

	def modifyTheList(self, head):
		a = []
		temp = head
		while temp != None:
			a.append(temp.data)
			temp = temp.next
		i = 0
		v = [0] * len(a)
		while i < len(a) // 2:
			v[i] = a[len(a) - 1 - i] - a[i]
			i = i + 1
		i = len(a) // 2
		if len(a) % 2 == 0:
			j = len(a) // 2 - 1
		else:
			j = len(a) // 2
		while i < len(a) and j >= 0:
			v[i] = a[j]
			i = i + 1
			j = j - 1
		i = 0
		temp = head
		while i < len(a):
			head.data = v[i]
			i = i + 1
			head = head.next
		return temp


def reverse(head):
	cur = head
	prev = None
	while cur:
		temp = cur.next
		cur.next = prev
		prev = cur
		cur = temp
	return prev

class Solution:

	def modifyTheList(self, head):
		slow = fast = head
		while fast and fast.next and fast.next.next:
			slow = slow.next
			fast = fast.next.next
		head2 = slow.next
		slow.next = None
		head2 = reverse(head2)
		temp1 = head
		temp2 = head2
		temp3 = None
		while temp1 and temp2:
			temp3 = temp1
			txt = temp3.data
			temp1.data = temp2.data - temp1.data
			temp2.data = txt
			temp1 = temp1.next
			temp2 = temp2.next
		if temp3 and temp3.next:
			temp3 = temp3.next
		head2 = reverse(head2)
		if temp3:
			temp3.next = head2
		return head


def reverse(head):
	pre = None
	while head:
		nxt = head.next
		head.next = pre
		pre = head
		head = nxt
	return pre

class Solution:

	def modifyTheList(self, head):
		slow = fast = head
		while fast and fast.next and fast.next.next:
			slow = slow.next
			fast = fast.next.next
		head2 = slow.next
		slow.next = None
		head2 = reverse(head2)
		tmp1 = head
		tmp2 = head2
		temp = None
		while tmp1 and tmp2:
			temp = tmp1
			txt = temp.data
			tmp1.data = tmp2.data - tmp1.data
			tmp2.data = txt
			tmp1 = tmp1.next
			tmp2 = tmp2.next
		if temp and temp.next:
			temp = temp.next
		head2 = reverse(head2)
		if temp:
			temp.next = head2
		return head


class Solution:

	def modifyTheList(self, head):
		l = []
		while head:
			l.append(head.data)
			head = head.next
		j = len(l) - 1
		l1 = []
		for i in range(len(l) // 2):
			l1.append(l[i])
			l[i] = l[j] - l[i]
			j -= 1
		k = len(l1) - 1
		if len(l) % 2:
			for t in range(len(l) // 2 + 1, len(l)):
				l[t] = l1[k]
				k -= 1
		else:
			for t in range(len(l) // 2, len(l)):
				l[t] = l1[k]
				k -= 1
		temp = Node(-1)
		a = temp
		n = len(l)
		i = 0
		while n:
			a.next = Node(l[i])
			i += 1
			a = a.next
			n -= 1
		return temp.next


class Solution:

	def modifyTheList(self, head):
		if head.next == None:
			return head
		else:
			stack = [head]
			while stack[-1].next:
				current = stack[-1]
				stack.append(stack[-1].next)
			length = len(stack)
			newHead = Node(stack[length - 1].data - stack[0].data)
			lastNode = newHead
			for i in range(1, length):
				if length - 1 - i > i:
					node = Node(stack[length - 1 - i].data - stack[i].data)
					lastNode.next = node
					lastNode = node
				elif length - 1 - i < i:
					node = Node(stack[length - 1 - i].data)
					lastNode.next = node
					lastNode = node
				else:
					node = Node(stack[i].data)
					lastNode.next = node
					lastNode = node
			return newHead


class Solution:

	def modifyTheList(self, head):
		b = []
		c = []
		tmp = head
		while tmp:
			b.append(tmp.data)
			c.append(tmp)
			tmp = tmp.next
		n = len(b) // 2
		x = len(c)
		tmp = head
		for i in range(n):
			(c[x - 1 - i].data, c[i].data) = (c[i].data, c[x - 1 - i].data - c[i].data)
		return head


class Solution:

	def modifyTheList(self, head):
		l = []
		t = head
		while t:
			l.append(t.data)
			t = t.next
		t = head
		for i in range(len(l) // 2):
			t.data = l[-i - 1] - l[i]
			t = t.next
		if len(l) % 2:
			t = t.next
		j = 0
		l = list(reversed(l[:len(l) // 2]))
		while t:
			t.data = l[j]
			j += 1
			t = t.next
		return head


class Solution:

	def modifyTheList(self, head):
		lis = []
		itr = head
		while itr:
			lis.append(itr.data)
			itr = itr.next
		mid = len(lis) // 2
		liss = lis.copy()
		for i in range(mid + 1):
			lis[i] = lis[len(lis) - i - 1] - lis[i]
		for i in range(mid, len(lis)):
			lis[i] = liss[len(liss) - i - 1]
		ans = pre = Node(-1)
		for i in lis:
			pre.next = Node(i)
			pre = pre.next
		return ans.next


class Solution:

	def modifyTheList(self, head):
		temp = head
		cnt = 0
		lst = []
		while temp != None:
			cnt += 1
			lst.append(temp.data)
			temp = temp.next
		i = 0
		j = cnt - 1
		while i < j:
			k = lst[i]
			lst[i] = lst[j] - lst[i]
			lst[j] = k
			i += 1
			j -= 1
		k = 0
		temp = head
		while temp != None:
			temp.data = lst[k]
			temp = temp.next
			k += 1
		return head


import itertools

class Solution:

	def modifyTheList(self, head):
		p = head
		p1 = head
		pnext = head
		l1 = []
		l2 = []
		while p:
			l1.append(p.data)
			p = p.next
		while pnext and pnext.next:
			l2.append(p1.data)
			p1 = p1.next
			pnext = pnext.next.next
		l1.reverse()
		dummy = Node(0)
		n0 = dummy
		for (i, j) in itertools.zip_longest(l1, l2):
			n0.next = Node(i - int(j or 0))
			n0 = n0.next
		return dummy.next


class Solution:

	def modifyTheList(self, head):
		l = []
		while head:
			l.append(head.data)
			head = head.next
		mid = len(l) // 2
		i = 0
		j = len(l) - 1
		while i < j:
			(l[i], l[j]) = (l[j], l[i])
			l[i] = l[i] - l[j]
			i += 1
			j -= 1
		for i in l:
			print(i, end=' ')


class Solution:

	def modifyTheList(self, head):
		l = []
		start = head
		while head:
			l.append(head.data)
			head = head.next
		head = start
		n = len(l)
		for _ in range(n // 2):
			head.data = l.pop() - head.data
			head = head.next
		if n % 2:
			head = head.next
			l.pop()
		while head:
			head.data = l.pop()
			head = head.next
		return start


class Solution:

	def modifyTheList(self, head):
		(k, nl) = ([], Node(0))
		while head:
			k.append(head.data)
			head = head.next
		r = len(k)
		for i in range(r // 2):
			t = k[r - i - 1]
			k[r - i - 1] = k[i]
			k[i] = t - k[i]
		ns = nl
		for s in range(r):
			nl.next = Node(k[s])
			nl = nl.next
		return ns.next


class Solution:

	def modifyTheList(self, head):
		slow = head
		fast = head.next
		while fast and fast.next:
			slow = slow.next
			fast = fast.next.next
		head2 = slow.next
		slow.next = None
		curr = head2
		prev = None
		while curr != None:
			nextt = curr.next
			curr.next = prev
			prev = curr
			curr = nextt
		ptr1 = head
		ptr2 = prev
		while ptr2 != None:
			temp = ptr1.data
			ptr1.data = ptr2.data - ptr1.data
			ptr2.data = temp
			ptr1 = ptr1.next
			ptr2 = ptr2.next
		curr = prev
		pre = None
		while curr != None:
			nextt = curr.next
			curr.next = pre
			pre = curr
			curr = nextt
		slow.next = pre
		return head


class Solution:

	def modifyTheList(self, head):
		c = 0
		temp = head
		while temp:
			temp = temp.next
			c += 1
		if head:
			r = c
			temp = head
			l = []
			while temp:
				l.append(temp.data)
				temp = temp.next
			t1 = head
			i = 1
			l1 = 0
			while t1 != None:
				t1.data = l[-i] - t1.data
				l1 += 1
				r -= 1
				if l1 > r:
					t1.data = l[-i]
				i += 1
				t1 = t1.next
			return head


import copy

def reverse(head):
	temp = head
	t1 = None
	while temp.next != None:
		t2 = temp
		temp = temp.next
		t2.next = t1
		t1 = t2
	temp.next = t1
	head = temp
	return head

def count(head):
	c = 0
	temp = head
	while temp:
		temp = temp.next
		c += 1
	return c

class Solution:

	def modifyTheList(self, head):
		if head:
			r = count(head)
			temp = head
			l = []
			while temp:
				l.append(temp.data)
				temp = temp.next
			t1 = head
			i = 1
			l1 = 0
			while t1 != None:
				t1.data = l[-i] - t1.data
				l1 += 1
				r -= 1
				if l1 > r:
					t1.data = l[-i]
				i += 1
				t1 = t1.next
			return head


class Solution:

	def modifyTheList(self, head):
		l = Linked_List()
		t = head
		d = None
		while t != None:
			l.insert(t.data)
			n = t.next
			t.next = d
			d = t
			t = n
		head = d
		slow = l.head
		fast = l.head
		t = head
		while fast != None and fast.next != None:
			t.data = t.data - slow.data
			t = t.next
			slow = slow.next
			fast = fast.next.next
		return head


class Solution:

	def modifyTheList(self, head):
		h = head
		li = []
		li1 = []
		while h:
			li.append(h.data)
			h = h.next
		l = len(li) // 2
		for i in range(l):
			r = li[-(i + 1)] - li[i]
			li1.append(r)
		if len(li) % 2 == 0:
			l = l - 1
			for j in range(l, -1, -1):
				li1.append(li[j])
		else:
			for j in range(l, -1, -1):
				li1.append(li[j])
		h = head
		j = 0
		while h:
			h.data = li1[j]
			j += 1
			h = h.next
		return head


def reverse_list(head):
	curr = head
	if curr is None or curr.next == None:
		return curr
	small_head = reverse_list(curr.next)
	tail = curr.next
	tail.next = curr
	curr.next = None
	return small_head

def mid(head):
	curr = head
	c = 0
	while curr is not None:
		c += 1
		curr = curr.next
	return c // 2

class Solution:

	def modifyTheList(self, head):
		arr = []
		reversed = reverse_list(head)
		while reversed is not None:
			arr.append(reversed.data)
			reversed = reversed.next
		i = 0
		while i < len(arr) // 2:
			arr[i] = arr[i] - arr[len(arr) - i - 1]
			i += 1
		head = tail = Node(arr[0])
		for i in range(1, len(arr)):
			tail.next = Node(arr[i])
			tail = tail.next
		return head


class Solution:

	def modifyTheList(self, head):
		l = []
		temp = head
		while temp != None:
			l.append(temp.data)
			temp = temp.next
		for i in range(len(l) // 2):
			(l[i], l[len(l) - i - 1]) = (l[len(l) - i - 1] - l[i], l[i])
		temp = head
		for i in l:
			temp.data = i
			temp = temp.next
		return head


class Solution:

	def modifyTheList(self, head):
		curr = head
		arr = []
		while curr:
			arr.append(curr.data)
			curr = curr.next
		curr = head
		for i in range(len(arr) // 2):
			curr.data = arr.pop() - arr[i]
			curr = curr.next
		for i in range(len(arr) - 1, -1, -1):
			curr.data = arr[i]
			curr = curr.next
		return head


class Solution:

	def modifyTheList(self, head):
		temp = head
		start = head
		l = []
		while temp:
			l.append(temp.data)
			temp = temp.next
		m = len(l) // 2
		for i in range(m):
			head.data = l[len(l) - i - 1] - l[i]
			head = head.next
		if len(l) % 2 == 0:
			n = m
		else:
			n = m + 1
		l = l[:n][::-1]
		for i in range(n):
			head.data = l[i]
			head = head.next
		return start


class Solution:

	def modifyTheList(self, head):
		if not head.next:
			return head

		def reverse(mid):
			prev = None
			temp = mid.next
			while temp:
				(prev, mid.next) = (mid, prev)
				mid = temp
				temp = temp.next
			mid.next = prev
			return mid
		(mid, end) = (head, head)
		while end and end.next:
			join = mid
			mid = mid.next
			end = end.next.next
		if end:
			join = mid
			mid = mid.next
		join.next = None
		mid = reverse(mid)
		temp = mid
		st1 = head
		while temp:
			(st1.data, temp.data) = (temp.data - st1.data, st1.data)
			st1 = st1.next
			temp = temp.next
		join.next = reverse(mid)
		return head


class Solution:

	def modifyTheList(self, head):
		l = []
		n = []
		res = []
		while head is not None:
			l.append(head.data)
			head = head.next
		m = [0] * len(l)
		if len(l) % 2 == 1:
			for i in range(len(l) // 2):
				m[i] = l[len(l) - i - 1] - l[i]
			m[len(l) // 2] = l[len(l) // 2]
			for i in range(len(l) // 2 + 1, len(l)):
				m[i] = l[len(l) - i - 1]
		else:
			for i in range(len(l) // 2):
				m[i] = l[len(l) - i - 1] - l[i]
			for i in range(len(l) // 2, len(l)):
				m[i] = l[len(l) - i - 1]
		L = Linked_List()
		for i in m:
			L.insert(i)
		return L.head


class Solution:

	def modifyTheList(self, head):
		if head.next is None:
			return head
		slow = head
		fast = head
		while fast.next and fast.next.next:
			slow = slow.next
			fast = fast.next.next
		right = slow.next
		left = head
		right = reverse(right)
		slow.next = None
		ptr1 = left
		ptr2 = right
		while ptr1 and ptr2:
			temp = ptr1.data
			ptr1.data = ptr2.data - ptr1.data
			ptr2.data = temp
			if ptr1.next == None and ptr2.next == None:
				break
			ptr1 = ptr1.next
			ptr2 = ptr2.next
		right = reverse(right)
		ptr1.next = right
		return head

def reverse(head):
	if head is None:
		return head
	curr = head
	prev = None
	while curr:
		nxt = curr.next
		curr.next = prev
		prev = curr
		curr = nxt
	return prev


class Solution:

	def modifyTheList(self, head):
		l = []
		l2 = []
		root = head
		while root:
			l.append(root.data)
			root = root.next
		t = int(len(l) / 2)
		for k in range(len(l)):
			if k < t:
				a = l[-(k + 1)] - l[k]
				l2.append(a)
			else:
				l2.append(l[len(l) - 1 - k])
		x = 0
		v = head
		while v:
			v.data = l2[x]
			x = x + 1
			v = v.next
		return head


class Solution:

	def modifyTheList(self, head):
		l = []
		l2 = []
		root = head
		while root:
			l.append(root.data)
			root = root.next
		i = 0
		n = head
		for i in range(len(l)):
			d = l[-(i + 1)]
			if i < len(l) // 2:
				n.data = d - n.data
			else:
				n.data = d
			n = n.next
		return head


class Solution:

	def modifyTheList(self, head):
		curr = head
		fast = head
		a = []
		b = []
		while curr:
			curr = curr.next
		curr = head
		while fast and fast.next:
			a.append(curr.data)
			curr = curr.next
			fast = fast.next.next
		if fast != None:
			c = [curr.data]
			curr = curr.next
		else:
			c = []
		j = 0
		a = a[::-1]
		while curr:
			b.append(curr.data - a[j])
			j += 1
			curr = curr.next
		b = b[::-1]
		final = b + c + a
		curr = head
		head = curr
		j = 0
		while curr:
			curr.data = final[j]
			j += 1
			curr = curr.next
		return head


class Solution:

	def modifyTheList(self, head):
		values = []
		curr = head
		size = 0
		while curr:
			size += 1
			values.append(curr.data)
			curr = curr.next
		i = 0
		while i < n // 2:
			temp = values[i]
			values[i] = values[size - 1 - i]
			values[size - 1 - i] = temp
			values[i] = values[i] - values[size - 1 - i]
			i += 1
		curr = head
		i = 0
		while curr:
			curr.data = values[i]
			i += 1
			curr = curr.next
		return head


class Solution:

	def modifyTheList(self, head):
		(l, t) = ([], head)
		while t:
			l.append(t.data)
			t = t.next
		(t, i) = (head, 0)
		n = len(l)
		while t:
			if i < n // 2:
				t.data = l.pop() - t.data
				i += 1
			else:
				t.data = l.pop()
				i += 1
			t = t.next
		return head


class Solution:

	def modifyTheList(self, head):
		l = []
		n = head
		while True:
			l.append(n.data)
			if not n.next:
				break
			n = n.next
		i = 0
		n = head
		for i in range(len(l)):
			d = l[-(i + 1)]
			if i < len(l) // 2:
				n.data = d - n.data
			else:
				n.data = d
			n = n.next
		return head


class Solution:

	def modifyTheList(self, head):
		p = head
		l = []
		while p:
			l.append(p.data)
			p = p.next
		e = len(l) - 1
		s = 0
		while s < e:
			(l[s], l[e]) = (l[e] - l[s], l[s])
			s = s + 1
			e = e - 1
		p = head
		s = 0
		while p:
			p.data = l[s]
			s = s + 1
			p = p.next
		return head


class Solution:

	def getrev(self, h):
		cur = h
		prev = None
		while cur:
			n = cur.next
			cur.next = prev
			prev = cur
			cur = n
		return prev

	def getmid(self, head):
		slow = head
		fast = head
		while fast.next and fast.next.next:
			slow = slow.next
			fast = fast.next.next
		return slow

	def modifyTheList(self, head):
		h1 = head
		m = self.getmid(head)
		m.next = h2 = self.getrev(m.next)
		while h2:
			tmp = h1.data
			t = h2.data
			h1.data = t - tmp
			h2.data = tmp
			h1 = h1.next
			h2 = h2.next
		m.next = self.getrev(m.next)
		return head


class Solution:

	def modifyTheList(self, head):
		current = head
		a = []
		while current != None:
			a.append(current.data)
			current = current.next
		p = []
		for i in range(len(a) - 1, -1, -1):
			p.append(a[i])
		for j in range(len(p) // 2):
			p[j] = p[j] - a[j]
		current = head
		count = 0
		while current != None:
			current.data = p[count]
			count += 1
			current = current.next
		return head

class Node:

	def __init__(self, val):
		self.data = val
		self.next = None

class Linked_List:

	def __init__(self):
		self.head = None
		self.tail = None

	def insert(self, val):
		if self.head is None:
			self.head = Node(val)
			self.tail = self.head
		else:
			self.tail.next = Node(val)
			self.tail = self.tail.next

def printList(head):
	tmp = head
	while tmp:
		print(tmp.data, end=' ')
		tmp = tmp.next
	print()
if __name__ == '__main__':
	for i in range(int(input())):
		n = int(input())
		arr = [int(x) for x in input().split()]
		lis = Linked_List()
		for i in arr:
			lis.insert(i)
		newHead = Solution().modifyTheList(lis.head)
		printList(newHead)


class Solution:

	def modifyTheList(self, head):
		li = []
		curr = head
		count = 0
		while curr != None:
			li.append(curr.data)
			count += 1
			curr = curr.next
		curr = head
		i = 0
		while li != []:
			if i < count // 2:
				curr.data = li.pop() - curr.data
			else:
				curr.data = li.pop()
			i += 1
			curr = curr.next
		return head

class Node:

	def __init__(self, val):
		self.data = val
		self.next = None

class Linked_List:

	def __init__(self):
		self.head = None
		self.tail = None

	def insert(self, val):
		if self.head is None:
			self.head = Node(val)
			self.tail = self.head
		else:
			self.tail.next = Node(val)
			self.tail = self.tail.next

def printList(head):
	tmp = head
	while tmp:
		print(tmp.data, end=' ')
		tmp = tmp.next
	print()
if __name__ == '__main__':
	for i in range(int(input())):
		n = int(input())
		arr = [int(x) for x in input().split()]
		lis = Linked_List()
		for i in arr:
			lis.insert(i)
		newHead = Solution().modifyTheList(lis.head)
		printList(newHead)


class Solution:

	def modifyTheList(self, head):
		l = []
		itr = head
		while itr:
			l.append(itr.data)
			itr = itr.next
		l = l[::-1]
		itr = head
		for i in range(len(l) // 2):
			itr.data = l[i] - itr.data
			itr = itr.next
		for i in range(len(l) // 2, len(l)):
			itr.data = l[i]
			itr = itr.next
		return head

class Node:

	def __init__(self, val):
		self.data = val
		self.next = None

class Linked_List:

	def __init__(self):
		self.head = None
		self.tail = None

	def insert(self, val):
		if self.head is None:
			self.head = Node(val)
			self.tail = self.head
		else:
			self.tail.next = Node(val)
			self.tail = self.tail.next

def printList(head):
	tmp = head
	while tmp:
		print(tmp.data, end=' ')
		tmp = tmp.next
	print()
if __name__ == '__main__':
	for i in range(int(input())):
		n = int(input())
		arr = [int(x) for x in input().split()]
		lis = Linked_List()
		for i in arr:
			lis.insert(i)
		newHead = Solution().modifyTheList(lis.head)
		printList(newHead)


class Stack:

	def __init__(self):
		self.a = []

	def push(self, data):
		self.a.append(data)

	def pop(self):
		return self.a.pop()

	def isEmpty(self):
		return len(self.a) == 0

class Solution:

	def modifyTheList(self, head):
		n = 0
		p = head
		s = Stack()
		while p:
			s.push(p.data)
			p = p.next
			n = n + 1
		p = head
		t = 1
		while t <= n // 2:
			p.data = s.pop() - p.data
			p = p.next
			t = t + 1
		while p:
			p.data = s.pop()
			p = p.next
		return head

class Node:

	def __init__(self, val):
		self.data = val
		self.next = None

class Linked_List:

	def __init__(self):
		self.head = None
		self.tail = None

	def insert(self, val):
		if self.head is None:
			self.head = Node(val)
			self.tail = self.head
		else:
			self.tail.next = Node(val)
			self.tail = self.tail.next

def printList(head):
	tmp = head
	while tmp:
		print(tmp.data, end=' ')
		tmp = tmp.next
	print()
if __name__ == '__main__':
	for i in range(int(input())):
		n = int(input())
		arr = [int(x) for x in input().split()]
		lis = Linked_List()
		for i in arr:
			lis.insert(i)
		newHead = Solution().modifyTheList(lis.head)
		printList(newHead)


class Solution:

	def modifyTheList(self, head):
		current = head
		count = 1
		current2 = None
		while current.next:
			count += 1
			current = current.next
		if count == 1:
			return head
		count_half = count // 2
		current = head
		while count_half - 1:
			current = current.next
			count_half -= 1
		if count % 2 != 0:
			current = current.next
		current.next = self.reverseList(current.next)
		current2 = current.next
		current = head
		while current2:
			temp = current.data
			temp2 = current2.data
			current.data = temp2 - temp
			current2.data = temp
			current2 = current2.next
			current = current.next
		count_half = count // 2
		current = head
		while count_half - 1:
			current = current.next
			count_half -= 1
		if count % 2 != 0:
			current = current.next
		current.next = self.reverseList(current.next)
		return head

	def reverseList(self, head):
		current = head
		prev = None
		while current:
			Next = current.next
			current.next = prev
			prev = current
			current = Next
		return prev


class Solution:

	def reverse(self, head):
		curr = head
		prev = None
		while curr:
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
		return prev

	def diffrence(self, head1, head2):
		curr1 = head1
		curr2 = head2
		nll = Linked_List()
		while curr1 and curr2:
			nll.insert(curr2.data - curr1.data)
			curr1 = curr1.next
			curr2 = curr2.next
		return nll.head

	def modifyTheList(self, head):
		fast = head
		slow = head
		prev_slow = head
		midnode = None
		res = True
		if not head.next:
			return head
		else:
			while fast and fast.next:
				fast = fast.next.next
				prev_slow = slow
				slow = slow.next
			if fast:
				midnode = slow
				slow = slow.next
			secondhalf = slow
			prev_slow.next = None
			firsthalf = head
			secondhalf = self.reverse(secondhalf)
			newfirst = self.diffrence(firsthalf, secondhalf)
			newsecond = self.reverse(firsthalf)
			last_first = newfirst
			while last_first.next:
				last_first = last_first.next
			prev_slow = last_first
			if midnode:
				prev_slow.next = midnode
				midnode.next = newsecond
			else:
				prev_slow.next = newsecond
			return newfirst


class Solution:

	def reverse(self, head):
		prev = None
		while head:
			temp = head.next
			head.next = prev
			prev = head
			head = temp
		return prev

	def findMid(self, head):
		slow = head
		fast = head
		while fast and fast.next:
			fast = fast.next.next
			slow = slow.next
		return slow

	def modifyTheList(self, head):
		if not head.next:
			return head
		mid = self.findMid(head)
		end = self.reverse(mid)
		back = end
		front = head
		while back:
			temp = front.data
			front.data = back.data - front.data
			back.data = temp
			back = back.next
			front = front.next
		self.reverse(end)
		return head


class Solution:

	def modifyTheList(self, head):
		if head == None or head.next == None:
			return head
		n = 0
		st = []
		temp = head
		while temp != None:
			n += 1
			st.append(temp.data)
			temp = temp.next
		n = n // 2
		k = 0
		curr = head
		while k < n:
			temp = st.pop()
			curr.data = temp - curr.data
			curr = curr.next
			k += 1
		while curr != None and len(st):
			curr.data = st.pop()
			curr = curr.next
		return head


class Solution:

	def modifyTheList(self, head):
		(a, b) = ([], [])
		temp = head
		while temp:
			a.append(temp.data)
			temp = temp.next
		n = len(a)
		h = n - 1
		for i in range(n // 2):
			b.append(a[h] - a[i])
			h -= 1
		for i in range(n // 2, n):
			b.append(a[h])
			h -= 1
		temp = head
		for i in range(n):
			temp.data = b[i]
			temp = temp.next
		return head


class Solution:

	def recur(self, start, end):
		if end:
			self.recur(start, end.next)
		else:
			return
		v1 = start[0].data
		v2 = end.data
		start[0].data = v2 - v1
		start[0] = start[0].next
		end.data = v1

	def modifyTheList(self, head):
		fp = head
		sp = head
		while fp and fp.next:
			sp = sp.next
			fp = fp.next.next
		if fp:
			sp = sp.next
		start = head
		self.recur([start], sp)
		return head


class Solution:

	def modifyTheList(self, head):
		temp = head
		lst = []
		while temp:
			lst.append(temp.data)
			temp = temp.next
		n = len(lst)
		mid = n // 2
		new_array = [0 for _ in range(n)]
		lst1 = lst[:mid]
		lst2 = lst1[::-1]
		if n % 2 == 1:
			new_array[mid] = lst[mid]
			for i in range(mid):
				new_array[i] = lst[n - i - 1] - lst[i]
			for i in range(mid + 1, n):
				new_array[i] = lst2[i - mid - 1]
		elif n % 2 == 0:
			for i in range(mid):
				new_array[i] = lst[n - i - 1] - lst[i]
			for i in range(mid):
				new_array[mid + i] = lst2[i]
		temp = head
		for i in range(len(new_array)):
			temp.data = new_array[i]
			temp = temp.next
		return head


class Solution:

	def modifyTheList(self, head):
		s = head
		f = head
		while f != None and f.next != None:
			s = s.next
			f = f.next.next
		(curr, prev, nex) = (s, None, s)
		while nex != None:
			nex = curr.next
			curr.next = prev
			prev = curr
			curr = nex
		ptr = head
		last = prev
		while prev != None:
			k = ptr.data
			ptr.data = prev.data - ptr.data
			prev.data = k
			ptr = ptr.next
			prev = prev.next
		(curr, prev, nex) = (last, None, last)
		while nex != None:
			nex = curr.next
			curr.next = prev
			prev = curr
			curr = nex
		return head


class Solution:

	def modifyTheList(self, head):
		i = 0
		j = 0
		slow = head
		fast = head
		while fast:
			if (i + 1) // 2 > j:
				slow = slow.next
				j += 1
			fast = fast.next
			i += 1
		middle = slow
		length = i
		prev = middle
		if prev:
			curr = prev.next
		else:
			curr = None
		while curr:
			tmp = curr.next
			curr.next = prev
			prev = curr
			curr = tmp
		start = head
		end = prev
		curr_len = 0
		while curr_len < length // 2:
			tmp_data = start.data
			start.data = end.data - tmp_data
			end.data = tmp_data
			curr_len += 1
			start = start.next
			end = end.next
		rev_start = prev
		rev_prev = None
		while rev_start != middle:
			tmp = rev_start.next
			rev_start.next = rev_prev
			rev_prev = rev_start
			rev_start = tmp
		return head


class Solution:

	def modifyTheList(self, head):
		if head is None:
			return
		if head.next is None:
			return head

		def reverseLL(head):
			cur = head
			prev = None
			nex = None
			while cur != None:
				nex = cur.next
				cur.next = prev
				prev = cur
				cur = nex
			return prev
		(slow, fast) = (head, head)
		while fast.next.next is not None:
			slow = slow.next
			fast = fast.next.next
			if fast.next is None:
				break
		head2 = slow.next
		slow.next = None
		head2 = reverseLL(head2)
		(cur2, cur) = (head2, head)
		while cur2.next is not None:
			cur2.data -= cur.data
			(cur2, cur) = (cur2.next, cur.next)
		cur2.data -= cur.data
		head = reverseLL(head)
		cur2.next = head
		return head2


class Solution:

	def modifyTheList(self, head):
		if head is None:
			return
		stack = []
		(fast, slow) = (head, head)
		while fast != None and fast.next != None:
			fast = fast.next.next
			slow = slow.next
		x = 1
		if fast == None:
			x = 1
		else:
			slow = slow.next
		while slow != None:
			stack.append(slow)
			slow = slow.next
		fast = head
		while stack:
			stack.pop().data -= fast.data
			fast = fast.next
		cur = head
		prev = None
		nex = None
		while cur != None:
			nex = cur.next
			cur.next = prev
			prev = cur
			cur = nex
		return prev


class Solution:

	def modifyTheList(self, head):

		def mid(h):
			s = h
			f = h
			while f.next and f.next.next:
				s = s.next
				f = f.next.next
			return s

		def rev(h):
			if h == None or h.next == None:
				return h
			nh = rev(h.next)
			h.next.next = h
			h.next = None
			return nh

		def length(h):
			c = 0
			while h != None:
				c += 1
				h = h.next
			return c
		m = mid(head)
		n = length(head)
		sh = m.next
		m.next = None
		head = rev(head)
		if n % 2 != 0:
			h = head.next
		else:
			h = head
		while h != None:
			n = Node(sh.data - h.data)
			n.next = head
			head = n
			h = h.next
			sh = sh.next
		return head


class Solution:

	def modifyTheList(self, head):
		start = head
		temp = head
		myList = []
		while temp != None:
			myList.append(temp.data)
			temp = temp.next
		i = 0
		j = len(myList) - 1
		while i < j:
			Differnece = myList[j] - myList[i]
			myList[j] = myList[i]
			myList[i] = Differnece
			i = i + 1
			j = j - 1
		L = Linked_List()
		for i in myList:
			L.insert(i)
		return L.head


class Solution:

	def length(self, head):
		count = 0
		temp = head
		while temp != None:
			temp = temp.next
			count += 1
		return count

	def mid(self, head):
		slow = head
		fast = head
		prev = None
		while fast != None and fast.next != None:
			prev = slow
			slow = slow.next
			fast = fast.next.next
		return [prev, slow]

	def modifyTheList(self, head):
		if head == None or head.next == None:
			return head
		arr = []
		temp = head
		while temp != None:
			arr.append(temp.data)
			temp = temp.next
		arr = arr[::-1]
		k = 0
		temp = head
		[prevmid, mid] = self.mid(head)
		n = self.length(head)
		while temp != mid:
			temp.data = arr[k] - temp.data
			k += 1
			temp = temp.next
		if n % 2 == 0:
			mid.data = arr[k]
		k += 1
		temp = mid.next
		while temp != None:
			temp.data = arr[k]
			k += 1
			temp = temp.next
		return head


class Solution:

	def modifyTheList(self, head):
		ans = []
		newll = Linked_List()
		while head:
			ans.append(head.data)
			head = head.next
		for i in range(len(ans)):
			if i < len(ans) // 2:
				newll.insert(ans[len(ans) - i - 1] - ans[i])
			else:
				newll.insert(ans[len(ans) - i - 1])
		return newll.head


class Solution:

	def modifyTheList(self, head):
		n = head
		list1 = []
		while n != None:
			list1.append(n.data)
			n = n.next
		length = len(list1)
		i = 0
		j = length - 1
		while i < j:
			temp = list1[i]
			list1[i] = list1[j] - list1[i]
			list1[j] = temp
			i += 1
			j -= 1
		m = Node(list1[0])
		head = m
		for i in range(1, length):
			hh = Node(list1[i])
			m.next = hh
			m = m.next
		return head


class Solution:

	def modifyTheList(self, head):
		fast = head
		items = []
		while fast != None:
			items.append(fast.data)
			fast = fast.next
		if len(items) % 2 == 0:
			first = items[0:len(items) // 2]
			last = items[len(items) // 2:]
			last = last[::-1]
			items = items[::-1]
			for i in range(0, len(items) // 2):
				items[i] = last[i] - first[i]
		else:
			first = items[0:len(items) // 2]
			last = items[len(items) // 2 + 1:]
			last = last[::-1]
			items = items[::-1]
			for i in range(0, len(items) // 2):
				items[i] = last[i] - first[i]
		t = head
		for i in items:
			t.data = i
			t = t.next
		return head


class Solution:

	def modifyTheList(self, head):
		p = head
		f = head
		c = 0
		while f != None and f.next != None:
			p = p.next
			f = f.next.next
			c = c + 1
		middle = p
		if f == None:
			count = c * 2 + 1
		else:
			count = c * 2
		li = []
		l = []
		if count % 2 == 0:
			p = p.next
			middle = p
		while p != None:
			li.append(p.data)
			p = p.next
		temp = head
		for i in range(0, count // 2):
			t = li.pop()
			l.append(temp.data)
			temp.data = t - temp.data
			temp = temp.next
		for i in range(0, count // 2):
			t = l.pop()
			middle.data = t
			middle = middle.next
		return head