标签:turn only ever node color algorithm nod lin tno
Given a linked list, swap every two adjacent nodes and return its head.
Example:
Given1->2->3->4
, you should return the list as2->1->4->3
.
Note:
迭代,记得画个图,不然会懵逼
class Solution { public ListNode swapPairs(ListNode head) { ListNode dummy = new ListNode(-1), pre = dummy; dummy.next = head; while (pre.next !=null && pre.next.next!=null) { ListNode t = pre.next.next; pre.next.next = t.next; t.next = pre.next; pre.next = t; pre = t.next; } return dummy.next; } }
标签:turn only ever node color algorithm nod lin tno
原文地址:https://www.cnblogs.com/wentiliangkaihua/p/10337287.html