[Problem]
Given a linked list, swap every, adjacent nodes and return its head.
For example,
Given 1->2->3->4 , you should return the list as 2->1->4->3 .
Your algorithm should use only constant space. Modify the values in the list, only nodes itself can be changed.
[Analysis]
This problem can be solved simply by using recursion, but the process of thinking is a bit of a detour. The topic requirements can not change the value of the list, that is, only on the pointer on the node, observing the law, you can find that the need to do is to two node interchange position, and next set to the next set of two nodes after the first, thus constituting a recursive solution.
[Solution]
Public class Solution { public ListNode swappairs (ListNode head) { ifnull NULL ) { return head; } = Head.next; = Swappairs (head.next.next); = head; return node; }}
Leetcode #24 Swap Nodes in Pairs (M)