python listnode(给个链表,翻转相邻的节点,即0和1翻转,2和3翻转,用python)
本文目录
- 给个链表,翻转相邻的节点,即0和1翻转,2和3翻转,用python
- 为什么python3中运行list(map(None,l1,l2)) 报错TypeError: ’NoneType’ object is not callable
给个链表,翻转相邻的节点,即0和1翻转,2和3翻转,用python
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def swapPairs(self, head): “““ :type head: ListNode :rtype: ListNode “““ if head == None: return None; if head.next == None: #只有一个节点的情况 return head; node = head; result = head.next; #交换之后链表的头节点 while node and node.next: #还存在下一对节点 temp = node.next; #作节点交换处理 node.next = temp.next; temp.next = node; temp = node.next; if temp and temp.next: #如果下对节点有两个的话,当前这对节点第二个节点指向下对节点的第二个节点 node.next = temp.next; node = temp; return result;
为什么python3中运行list(map(None,l1,l2)) 报错TypeError: ’NoneType’ object is not callable
估计是solution的问题,改动一下代码就可以了:class Solution(object): def removeElements(self, head, val): “““ :type head: ListNode :type val: int :rtype: ListNode “““ cur = ListNode(0) cur.next = head p = cur cur.next=None while p.next: if p.next.val == val: cur.next=p.next p.next = p.next.next break p = p.next return cur.next
更多文章:

cloudera怎么读(不用cloudera manager怎么安装)
2025年3月18日 17:30

rocketmq事务消息(rocketmq 发送失败一般怎么处理)
2025年4月19日 11:00

软件配置管理工程师(软件配置管理,测试工程师,数据分析,项目经理助理)
2025年3月25日 07:00

strncpy实现(strncpy的用法(对于结构体指针数组,或者结构体二维数组))
2025年3月7日 13:30

vs2008激活码(Microsoft Visual Studio 2008在哪输入注册码)
2025年4月13日 01:10

oracle官方文档中文版(求一份中文版的oracle官方文档 尤其是SQL基础篇和PL/SQL篇)
2025年2月27日 10:40

cocktail lounge是什么意思(lounge是什么意思)
2025年2月15日 22:20

localstorage使用(java 怎么使用localstorage)
2025年2月12日 04:50

call me by your name(女生对男生说 call me by your name 是什么意思)
2025年2月17日 16:50

html背景颜色半透明(html整个网页在有一个背景图的情况下,怎么在盒子里加一个有透明度的背景,里面的图片和文字不要透明了)
2025年2月26日 22:20

theater攻略(cube escape theatre怎么玩)
2025年4月2日 23:10