subject : I'll give you a list , Two or two exchange the adjacent nodes , And return the head node of the linked list after exchange . You must complete this problem without modifying the value inside the node ( namely , Only node switching can be performed ).
Example 1:
Input :head = [1,2,3,4] Output :[2,1,4,3]
Example 2:Input :head = [] Output :[]
Example 3:Input :head = [1] Output :[1]
Tips :
The number of nodes in the linked list is in the range [0, 100] Inside 0 <= Node.val <= 100
Program description : If dummy nodes are not used , The code is like the following error code , If the linked list is disconnected ( According to the code drawing )
All the code :
Correct code :
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
p = ListNode(0)
p.next = head
cur = p
while cur.next and cur.next.next:
node1 = cur.next
node2 = cur.next.next
cur.next = node2
node1.next = node2.next
node2.next = node1
cur = node1
return p.next
Error code :
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def swapPairs(self, head: ListNode) -> ListNode:
if not head or not head.next:
return head
p = head
cur = head.next
q = cur.next
while cur.next:
p.next = q
cur.next = p
p = q
cur = p.next
q = cur.next
cur.next = p
return head
Title source : Power button (leetcode)
Python uses opencv to calibrate the camera to obtain parameters, and correct the image according to the distortion parameters. All codes are attached (smooth and painless version)
python利用opencvPerform camera c
Software use cases in micropython kernel development notebook: UART related experiments
Jane Medium : This paper gives