算法与数据结构之LeetCode刷题

0.前言

题目要求:算法与数据结构之LeetCode刷题——反转链表的相邻值

1.参考答案


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param a ListNode
    # @return a ListNode
    def swapPairs(self, head):
        dummy = ListNode(0)
        dummy.next = head
        pre, curr = dummy, head
        while curr and curr.next:       # curr =1, curr.next =2
            pre.next = curr.next        # 0 --> 2
            curr.next = pre.next.next   # 1 --> 3  # curr.next.next
            pre.next.next = curr        # 2 --> 1
            pre, curr = curr,curr.next  # pre = 1, curr= 3
        return dummy.next

参考网址:
https://www.cnblogs.com/asrman/p/3972094.html

相关文章:

  • 2022-01-01
  • 2022-12-23
  • 2021-07-06
  • 2022-12-23
  • 2021-04-24
  • 2021-06-11
  • 2021-04-14
猜你喜欢
  • 2021-12-16
  • 2021-12-16
  • 2021-06-13
  • 2022-01-09
  • 2021-11-25
  • 2021-07-29
  • 2022-12-23
相关资源
相似解决方案