Given a singly linked list of size N. The task is to swap elements in the linked list pairwise.
For example, if the input list is 1 2 3 4, the resulting list after swaps will be 2 1 4 3.
Note: You need to swap the nodes, not only the data. If only data is swapped then driver will print -1.
Example 1:
Input:
LinkedList: 1->2->2->4->5->6->7->8
Output: 2 1 4 2 6 5 8 7
Explanation: After swapping each pair
considering (1,2), (2, 4), (5, 6).. so
on as pairs, we get 2, 1, 4, 2, 6, 5,
8, 7 as a new linked list.
 
Example 2:
Input:
LinkedList: 1->3->4->7->9->10->1
Output: 3 1 7 4 10 9 1
Explanation: After swapping each pair
considering (1,3), (4, 7), (9, 10).. so
on as pairs, we get 3, 1, 7, 4, 10, 9,
1 as a new linked list.
Your Task:
The task is to complete the function pairWiseSwap() which takes the head node as the only argument and returns the head of modified linked list.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{3}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: """  list Node is as defined below:



class Node:

    def __init__(self, data):

		self.data = data

		self.next = None



"""



# complete this function

# return head of list after swapping

class Solution:    

    def pairWiseSwap(self, head):

        # code here



Solutions:
class Solution:

	def pairWiseSwap(self, head):
		i = 0
		curr = head
		prev = None
		while curr and i < 2:
			tmp = curr.next
			curr.next = prev
			prev = curr
			curr = tmp
			i += 1
		i = 0
		if tmp != None:
			head.next = self.pairWiseSwap(tmp)
		return prev


class Solution:

	def pairWiseSwap(self, head):
		prev = head
		curr = head
		succ = curr.next
		if succ != None:
			head = succ
		while curr != None and succ != None:
			prev.next = succ
			temp = succ.next
			succ.next = curr
			curr.next = temp
			prev = curr
			curr = temp
			if curr != None:
				succ = curr.next
		return head


class Solution:

	def pairWiseSwap(self, head):
		temp = head
		while temp and temp.next:
			x = temp.data
			temp.data = temp.next.data
			temp.next.data = x
			temp = temp.next.next
		while head:
			print(head.data, end=' ')
			head = head.next
		return None


class Solution:

	def pairWiseSwap(self, head):
		prev = head
		prev2 = None
		if prev.next is not None:
			cur = head.next
			head = cur
		while prev is not None and prev.next is not None:
			prev.next = cur.next
			cur.next = prev
			if prev2 is not None:
				prev2.next = cur
			prev2 = prev
			prev = prev.next
			if prev is None:
				return head
			else:
				cur = prev.next
		return head


class Solution:

	def pairWiseSwap(self, head):
		if head.next == None:
			return head
		x = Node(0)
		dummy = x
		cur = head
		while cur and cur.next:
			nxt = cur.next.next
			second = cur.next
			second.next = cur
			cur.next = nxt
			dummy.next = second
			dummy = cur
			cur = nxt
		return x.next


class Solution:

	def pairWiseSwap(self, head):
		i = 0
		h = head
		prev = None
		while h and i < 2:
			b = h.next
			h.next = prev
			prev = h
			h = b
			i += 1
		i = 0
		if b != None:
			head.next = self.pairWiseSwap(b)
		return prev


class Solution:

	def pairWiseSwap(self, head):
		if head == None:
			return None
		s = 2
		A = None
		B = head
		C = None
		while B and s:
			C = B.next
			B.next = A
			A = B
			B = C
			s = s - 1
		head.next = self.pairWiseSwap(B)
		return A


class Solution:

	def pairWiseSwap(self, head):
		dummy = Node(0)
		dummy.next = head
		prev = dummy
		curr = head
		while curr and curr.next:
			nxtPair = curr.next.next
			second = curr.next
			second.next = curr
			curr.next = nxtPair
			prev.next = second
			prev = curr
			curr = nxtPair
		return dummy.next


class Solution:

	def pairWiseSwap(self, head):
		if head is None or head.next is None:
			return head
		prev = head
		curr = head.next
		head = curr
		while True:
			nextPair = curr.next
			curr.next = prev
			if nextPair is None or nextPair.next is None:
				prev.next = nextPair
				break
			prev.next = nextPair.next
			prev = nextPair
			curr = prev.next
		return head


class Solution:

	def pairWiseSwap(self, head):
		return self.helper(head)

	def helper(self, head):
		if head is None or head.next is None:
			return head
		first = head
		second = head.next
		temp = self.helper(second.next)
		first.next = temp
		second.next = first
		return second


class Solution:

	def pairWiseSwap(self, head):
		if head == None or head.next == None:
			return head
		curr = head.next.next
		prev = head
		head = head.next
		head.next = prev
		while curr != None and curr.next != None:
			prev.next = curr.next
			prev = curr
			next = curr.next.next
			curr.next.next = curr
			curr = next
		prev.next = curr
		return head


class Solution:

	def pairWiseSwap(self, head):
		if head is None:
			return head
		if head.next is None:
			return head
		p1 = head
		p2 = head.next
		new_head = p2
		prev = None
		while True:
			if p1 is None:
				break
			if prev:
				if p2:
					prev.next = p2
			if p2 is None:
				break
			prev = p1
			p1.next = p2.next
			p2.next = p1
			p1 = p1.next
			if p1 is None:
				break
			p2 = p1.next
		return new_head


class Solution:

	def pairWiseSwap(self, head):
		first = head
		sec = head.next
		if sec:
			head = sec
		while sec:
			temp = sec.next
			sec.next = first
			if temp and temp.next:
				first.next = temp.next
				first = temp
				sec = temp.next
			else:
				first.next = temp
				break
		return head


class Solution:

	def pairWiseSwap(self, head):
		if head is None or head.next is None:
			return head
		prev = None
		curr = head
		next = None
		new_head = head.next
		while curr is not None and curr.next is not None:
			next = curr.next
			curr.next = next.next
			next.next = curr
			if prev is not None:
				prev.next = next
			prev = curr
			curr = curr.next
		return new_head


class Solution:

	def pairWiseSwap(self, head):
		temp = head
		temp1 = temp.next
		while temp and temp1:
			i = temp.data
			j = temp1.data
			temp.data = j
			temp1.data = i
			if temp.next.next and temp.next.next:
				temp = temp.next.next
				temp1 = temp1.next.next
			else:
				temp = temp.next
				temp1 = temp1.next
		temp2 = head
		while temp2:
			print(temp2.data, end=' ')
			temp2 = temp2.next


class Solution:

	def pairWiseSwap(self, head):
		if head is None or head.next is None:
			return head
		prev_node = head
		current_node = head.next
		new_head = current_node
		while True:
			next_node = current_node.next
			current_node.next = prev_node
			prev_node.next = next_node
			if next_node is None or next_node.next is None:
				break
			prev_node.next = next_node.next
			prev_node = next_node
			current_node = prev_node.next
		return new_head


class Solution:

	def pairWiseSwap(self, head):
		if not head or not head.next:
			return head
		pre = None
		curr = head
		nex = head.next
		nexnex = head.next.next
		head = head.next
		while curr and nex:
			curr.next = nexnex
			nex.next = curr
			if pre:
				pre.next = nex
			if nexnex and nexnex.next:
				pre = curr
				curr = nexnex
				nex = nexnex.next
				nexnex = nex.next
			else:
				break
		return head


class Solution:

	def pairWiseSwap(self, head):
		if head == None:
			return head
		k = head
		length = 0
		while k:
			length += 1
			k = k.next
		if length == 1:
			return head
		p = Node(0)
		prev = p
		cur = head
		while cur and cur.next:
			nxt = cur.next.next
			second = cur.next
			second.next = cur
			cur.next = nxt
			prev.next = second
			prev = cur
			cur = nxt
		return p.next


class Solution:

	def pairWiseSwap(self, start):
		if start.next is None:
			return start
		prev = start
		temp = prev
		curr = start.next
		last = None
		new_start = curr
		while curr:
			forward = curr.next
			curr.next = prev
			prev.next = forward
			prev = forward
			if prev:
				curr = prev.next
				temp.next = curr
				last = temp
				temp = prev
			else:
				curr = None
		if prev:
			last.next = prev
		return new_start


class Solution:

	def pairWiseSwap(self, start):
		if start.next is None:
			return start
		a = start
		b = start.next
		prev = None
		while a and b:
			forw = b.next
			a.next = forw
			if prev:
				prev.next = b
				b.next = a
			else:
				b.next = a
				start = b
			prev = a
			a = forw
			if a:
				b = a.next
		return start


class Solution:

	def pairWiseSwap(self, head):
		if head.next is None:
			return head
		temp = head.next.next
		head.next.next = head
		head = head.next
		head.next.next = temp
		n = head.next
		while n.next is not None:
			temp = n.next.next
			if temp is not None:
				temp1 = temp.next
				temp.next = n.next
				n.next = temp
				temp.next.next = temp1
				n = n.next.next
			else:
				break
		return head


class Solution:

	def pairWiseSwap(self, head):
		if not head or not head.next:
			return head
		prev = None
		cur = head
		while cur and cur.next:
			next_node = cur.next
			cur.next = next_node.next
			next_node.next = cur
			if not prev:
				head = next_node
			else:
				prev.next = next_node
			prev = cur
			cur = cur.next
		return head


class Solution:

	def pairWiseSwap(self, head):
		prev = head
		temp = None
		if head.next != None:
			curr = head.next
		else:
			return head
		prePrev = None
		while curr != None and prev.next != None:
			if prePrev != None:
				prePrev.next = curr
			temp = prev.next.next
			prev.next = temp
			curr.next = prev
			if prev == head:
				head = curr
			if temp != None and temp.next != None:
				curr = temp.next
				prePrev = prev
				prev = temp
			else:
				return head
		if temp != None:
			prev.next = temp
		return head


class Solution:

	def pairWiseSwap(self, head):
		temp = head
		k = 2
		cnt = 0
		curr = head
		prev = None
		next = None
		while curr != None and cnt < k:
			next = curr.next
			curr.next = prev
			prev = curr
			curr = next
			cnt += 1
		if next != None:
			head.next = self.pairWiseSwap(next)
		return prev


class Node:

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

class Solution:

	def pairWiseSwap(self, head):
		dummy = Node(0)
		dummy.next = head
		curr = head
		prev = dummy
		while curr and curr.next:
			prev.next = curr.next
			curr.next = curr.next.next
			prev.next.next = curr
			curr = curr.next
			prev = prev.next.next
		return dummy.next


class Solution:

	def pairWiseSwap(self, head):
		if head.next == None:
			return head
		temp = head
		prev = None
		x = None
		while temp != None and temp.next != None:
			secondnext = temp.next.next
			if x == None:
				x = temp.next
			first = temp
			if prev != None:
				prev.next = temp.next
			temp.next.next = temp
			first.next = secondnext
			temp = secondnext
			prev = first
		return x


class Solution:

	def pairWiseSwap(self, head):
		if not head:
			return head
		prev = head
		curr = head.next
		if prev and curr:
			prev.next = curr.next
			curr.next = prev
			head = curr
			tail = prev
			tail.next = self.pairWiseSwap(prev.next)
		return head


class Solution:

	def pairWiseSwap(self, head):
		prev = Node(0)
		dummy = prev
		prev.next = head
		while prev.next and prev.next.next:
			a = prev.next
			b = prev.next.next
			c = prev.next.next.next
			prev.next = b
			prev.next.next = a
			prev.next.next.next = c
			a.next = c
			prev = a
		return dummy.next


class Solution:

	def pairWiseSwap(self, head):
		if head is None or head.next is None:
			return head
		ptr1 = head
		ptr2 = head.next
		ptr1.next = ptr1.next.next
		ptr2.next = ptr1
		head = ptr2
		if ptr1.next is None:
			return head
		node1 = ptr1
		while node1 and node1.next:
			node2 = node1.next
			node3 = node1.next.next
			if node2 is None or node3 is None:
				break
			node1.next = node1.next.next
			node2.next = node2.next.next
			node3.next = node2
			node1 = node1.next.next
		return head


class Solution:

	def pairWiseSwap(self, head):
		if not head.next:
			return head
		tmp = head.next
		pre = None
		while head and head.next:
			temp = head.next.next
			head.next.next = head
			if pre:
				pre.next = head.next
			head.next = temp
			pre = head
			head = head.next
		return tmp


class Solution:

	def pairWiseSwap(self, head):
		a = head
		(b, c, prev) = (None, None, None)
		while a and a.next:
			b = a.next
			c = b.next
			if a == head:
				head = b
			else:
				prev.next = b
			b.next = a
			a.next = c
			prev = a
			a = c
		return head


class Solution:

	def pairWiseSwap(self, head):
		if head.next == None:
			return head
		curr = head
		fut = head.next
		head = fut
		prev = None
		while curr and fut:
			curr.next = fut.next
			fut.next = curr
			if prev != None:
				prev.next = fut
			prev = curr
			if curr and curr.next:
				curr = curr.next
				fut = curr.next
			else:
				break
		return head


class Solution:

	def pairWiseSwap(self, head):
		if head == None or head.next == None:
			return head
		a = head.next
		b = None
		curr = head
		while curr and curr.next:
			dummy = curr.next
			c = dummy.next
			curr.next = None
			dummy.next = curr
			if b != None:
				b.next = dummy
				b = dummy.next
			else:
				b = dummy.next
			curr = c
		if curr:
			b.next = curr
		return a


class Solution:

	def pairWiseSwap(self, head):
		i = 0
		h1 = head
		h2 = None
		while h1 and i < 2:
			temp = h1.next
			h1.next = h2
			h2 = h1
			h1 = temp
			i += 1
		i = 0
		if temp != None:
			head.next = self.pairWiseSwap(temp)
		return h2


class Solution:

	def pairWiseSwap(self, head):
		i = 0
		cur = head
		prev = None
		while cur and i < 2:
			temp = cur.next
			cur.next = prev
			prev = cur
			cur = temp
			i += 1
		i = 0
		if temp != None:
			head.next = self.pairWiseSwap(temp)
		return prev


class Node:

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

class Solution:

	def pairWiseSwap(self, head):
		self.head = head
		if self.head == None or self.head.next == None:
			return self.head
		prev = head
		cur = head.next
		t = cur
		prev1 = None
		while cur != None:
			temp = cur.next
			cur.next = prev
			prev1 = prev
			prev.next = temp
			prev = temp
			if temp:
				cur = temp.next
			else:
				cur = temp
			prev1.next = cur
		if temp != None:
			prev1.next = temp
		return t


class Solution:

	def pairWiseSwap(self, head):
		if not (head and head.next):
			return head
		(head.next.next, head.next, head) = (head, self.pairWiseSwap(head=head.next.next), head.next)
		return head


class Solution:

	def pairWiseSwap(self, head):
		prev = None
		cur = head
		while cur and cur.next:
			nxt = cur.next
			np = nxt.next
			if prev:
				prev.next = nxt
			else:
				head = nxt
			nxt.next = cur
			cur.next = np
			prev = cur
			cur = np
		return head


class Solution:

	def pairWiseSwap(self, head):
		if head.next == None:
			return head
		k = dummy = Node(0)
		dummy.next = head
		k = head.next
		try:
			while dummy.next and dummy.next.next:
				temp = head.next.next
				dummy.next = head.next
				dummy = dummy.next
				dummy.next = head
				dummy = dummy.next
				dummy.next = temp
				head = dummy.next
		except:
			pass
		return k


class Solution:

	def pairWiseSwap(self, head):
		h1 = None
		h2 = head
		while h2 and h2.next:
			n = h2.next
			h2.next = n.next
			n.next = h2
			if h1:
				h1.next = n
			if h2 == head:
				head = n
			h1 = h2
			h2 = h2.next
		return head


class Solution:

	def pairWiseSwap(self, node):
		if node is None or node.next is None:
			return node
		remaining = node.next.next
		newHead = node.next
		node.next.next = node
		node.next = self.pairWiseSwap(remaining)
		return newHead


class Solution:

	def pairWiseSwap(self, head):
		p = head
		q = head.next
		pre = Node(0)
		ans = pre
		pre.next = head
		while p and q:
			t = q.next
			q.next = p
			p.next = t
			pre.next = q
			pre = p
			if t is None:
				break
			p = t
			q = t.next
		return ans.next


class Node:

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

class Solution:

	def pairWiseSwap(self, head):
		if head == None or head.next == None:
			return head
		curr = head.next.next
		prev = head
		head = head.next
		head.next = prev
		while curr != None and curr.next != None:
			prev.next = curr.next
			prev = curr
			x = curr.next.next
			curr.next.next = curr
			curr = x
		prev.next = curr
		return head


class Solution:

	def pairWiseSwap(self, head):
		if head == None:
			return
		if head.next == None:
			return head
		prev = head
		cur = head.next
		t = cur
		prev1 = None
		while cur != None:
			temp = cur.next
			cur.next = prev
			prev1 = prev
			prev.next = temp
			prev = temp
			if temp:
				cur = temp.next
			else:
				cur = temp
			prev1.next = cur
		if temp != None:
			prev1.next = temp
		return t


class Solution:

	def pairWiseSwap(self, head):

class Solution:

	def pairWiseSwap(self, head):
		if not head or not head.next:
			return head
		next_node = head.next
		head.next = self.pairWiseSwap(next_node.next)
		next_node.next = head
		return next_node


class Solution:

	def pairWiseSwap(self, head):
		l = Node(0)
		temp2 = l
		prev = None
		while head:
			prev = None
			for i in range(2):
				if head is not None:
					temp = head
					head = head.next
					temp.next = prev
					prev = temp
			while temp2.next is not None:
				temp2 = temp2.next
			temp2.next = prev
		return l.next


class Solution:

	def pairWiseSwap(self, head):
		dummy = Node(0)
		dummy.next = head
		prev = dummy
		curr = head
		while curr and curr.next:
			next_pair_prev = curr.next.next
			next_pair_head = curr.next
			prev.next = next_pair_head
			next_pair_head.next = curr
			curr.next = next_pair_prev
			prev = curr
			curr = next_pair_prev
		return dummy.next


class Solution:

	def pairWiseSwap(self, head):
		a = 0
		prev = temp = head
		if temp.next == None:
			return head
		while temp != None:
			if a == 0:
				head = temp.next
				temp.next = temp.next.next
				head.next = temp
				prev = temp
				temp = temp.next
				a = 1
			elif temp.next == None:
				return head
			else:
				prev.next = temp.next
				temp.next = temp.next.next
				prev.next.next = temp
				prev = temp
				temp = temp.next
		return head


class Solution:

	def pairWiseSwap(self, head):
		dummy_start = Node(-1)
		dummy_start.next = head
		first = dummy_start
		third = head.next
		while third != None:
			first.next = third
			head.next = third.next
			third.next = head
			first = head
			head = head.next
			if head != None:
				third = head.next
			else:
				break
		return dummy_start.next


class Solution:

	def pairWiseSwap(self, head):
		if head is None or head.next is None:
			return head
		prev_node = None
		curr_node = head
		next_node = head.next
		while curr_node is not None and curr_node.next is not None:
			curr_node.next = next_node.next
			next_node.next = curr_node
			if prev_node is None:
				head = next_node
			else:
				prev_node.next = next_node
			prev_node = curr_node
			curr_node = curr_node.next
			if curr_node is not None:
				next_node = curr_node.next
		return head


class Solution:

	def pairWiseSwap(self, head):
		temp = head
		while temp != None and temp.next != None:
			k = temp.next.data
			temp.next.data = temp.data
			temp.data = k
			temp = temp.next.next
		temp = head
		while temp != None:
			print(temp.data, end=' ')
			temp = temp.next


class Solution:

	def pairWiseSwap(self, head):
		if head is None:
			return None
		if head.next is None:
			return head
		dummy = Node(0)
		dummy.next = head
		prev = dummy
		curr = head
		while curr != None and curr.next != None:
			first = curr.next
			second = curr.next.next
			curr.next = second
			first.next = curr
			prev.next = first
			prev = prev.next.next
			curr = curr.next
		return dummy.next


class Solution:

	def pairWiseSwap(self, head):
		t = head
		n = Node(0)
		l = n
		while t.next != None and t.next.next != None:
			n.next = Node(t.next.data)
			n = n.next
			n.next = Node(t.data)
			n = n.next
			t = t.next.next
		if t.next == None:
			n.next = Node(t.data)
		else:
			n.next = Node(t.next.data)
			n = n.next
			n.next = Node(t.data)
		l = l.next
		while l != None:
			print(l.data, end=' ')
			l = l.next


class Solution:

	def pairWiseSwap(self, head):
		a = head
		prev = None
		temp = a.next
		i = 0
		while i < 2 and a:
			temp = a.next
			a.next = prev
			prev = a
			a = temp
			i += 1
		if temp is not None:
			head.next = self.pairWiseSwap(temp)
		return prev


class Solution:

	def pairWiseSwap(self, head):
		first = ans = Node(0)
		first.next = head
		while first.next and first.next.next:
			second = first.next
			third = first.next.next
			first.next = third
			second.next = third.next
			third.next = second
			first = first.next.next
		return ans.next


class Solution:

	def pairWiseSwap(self, head):
		return self.swapUtil(head, None)

	def swapUtil(self, node, prev):
		if node is None:
			return node
		if node.next is None:
			return node
		first = node
		second = node.next
		temp = second.next
		second.next = first
		first.next = temp
		if prev is not None:
			prev.next = second
		self.swapUtil(temp, first)
		return second


class Solution:

	def pairWiseSwap(self, head):
		if head is None or head.next is None:
			return head
		dummy = Node(0)
		dummy.next = head
		prev = dummy
		curr = head
		while curr is not None and curr.next is not None:
			nex = curr.next
			curr.next = nex.next
			nex.next = curr
			prev.next = nex
			prev = curr
			curr = curr.next
		return dummy.next


class Solution:

	def pairWiseSwap(self, head):
		x = []
		while head:
			x.append(head.data)
			head = head.next
		if len(x) % 2 == 0:
			a = 0
			b = 1
			while b < len(x):
				(x[a], x[b]) = (x[b], x[a])
				a += 2
				b += 2
		else:
			a = 0
			b = 1
			while b < len(x) - 1:
				(x[a], x[b]) = (x[b], x[a])
				a += 2
				b += 2
		for i in x:
			print(i, end=' ')


class Solution:

	def pairWiseSwap(self, head):
		if head != None:
			if head.next != None:
				curr = head
				prev = None
				nextNode = curr.next
				while curr and nextNode:
					curr.next = nextNode.next
					nextNode.next = curr
					(curr, nextNode) = (nextNode, curr)
					if curr.next == head:
						head = curr
					else:
						prev.next = curr
					prev = nextNode
					curr = nextNode.next
					if curr:
						nextNode = curr.next
				return head
			else:
				return head
		else:
			return None


class Solution:

	def pairWiseSwap(self, head):
		if not head or not head.next:
			return head
		prev = head
		curr = head.next
		head = curr
		while True:
			temp = curr.next
			curr.next = prev
			if not temp or not temp.next:
				prev.next = temp
				break
			prev.next = temp.next
			prev = temp
			curr = prev.next
		return head