本文主要是介绍leetcode-24Swap Nodes in Pairs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
带头结点。
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/
public class Solution {public ListNode swapPairs(ListNode head){if(head == null) return null ;ListNode swaphead = new ListNode(0) ;swaphead.next = head ;ListNode fa = swaphead ;ListNode now = head , next ;while(now != null && now.next != null){next = now.next ;fa.next = next ;now.next = next.next ;next.next = now ;fa = now ;now = now.next ;}return swaphead.next ;}
}
这篇关于leetcode-24Swap Nodes in Pairs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!