题目信息

image.png

问题解答

https://leetcode-cn.com/submissions/detail/118746750/
image.png

  1. function swapPairs(head: ListNode | null): ListNode | null {
  2. const dummyHead = new ListNode()
  3. dummyHead.next = head
  4. let pre = dummyHead
  5. while(head && head.next) {
  6. let next = head.next
  7. //完成一次交换
  8. head.next = next.next
  9. next.next = head
  10. pre.next = next
  11. pre = head
  12. head = head.next
  13. }
  14. return dummyHead.next
  15. };