package question;public class Question24 {public static void main(String[] args) {//[1,2,3,4]ListNode l1 = new ListNode(1);ListNode l2 = new ListNode(2);ListNode l3 = new ListNode(3);ListNode l4 = new ListNode(4);l1.next=l2;l2.next=l3;l3.next=l4;new Question24().swapPairs(l1);}public ListNode swapPairs(ListNode head) {if (head==null||head.next==null) return head;ListNode left = head;ListNode right = head.next;ListNode pre = null;while (right!=null) {left.next=right.next;right.next=left;if (pre!=null) pre.next=right;else head = right;pre=left;left=left.next;if (left!=null) right=left.next;else right=null;}return head;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。