Swap Nodes In Pairs

来源:互联网 发布:c tryparse 源码 编辑:程序博客网 时间:2024/05/08 06:38

Given a linked list, swap every two 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. You may not modify the values in the list, only nodes itself can be changed.

public ListNode swapPairs(ListNode head) {// Start typing your Java solution below// DO NOT write main() functionif(head == null)return head;if(head.next == null)return head;ListNode odd = head;ListNode even = head.next;ListNode pre = head;odd.next = even.next;even.next = odd;head = even;pre = odd;while(odd.next != null && odd.next.next != null){odd = odd.next;even = odd.next;odd.next  = even.next;even.next = odd;pre.next = even;pre = odd;}return head;}