# LeetCode Python实现 链表简单部分

LeetCode Python实现 链表简单部分

node.val=node.next.val#当前值被后一个值覆盖
node.next=node.next.next#下一节点跳到下下一节点

这些关于next和val的解法从来没有见过，百度也没有搜索到，菜鸟教程也没有解释。

#2018-06-06  June Wednesday the 23 week, the 157 day SZ
'''

4 -> 5 -> 1 -> 9

•链表至少包含两个节点。
•链表中所有节点的值都是唯一的。
•给定的节点为非末尾节点并且一定是链表中的一个有效节点。
•不要从你的函数中返回任何结果。
'''

'''

'''
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
#拿到题没看懂出题意图，查了别人的答案才明白，这考点也是神奇
node.val=node.next.val#当前值被后一个值覆盖
node.next=node.next.next#下一节点跳到下下一节点

my_solution = Solution()
a = my_solution.deleteNode(1)
print(a)

'''

https://blog.csdn.net/qq_34364995/article/details/80546443
'''

'''

'''

'''

'''
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
"""
:type n: int
:rtype: ListNode
"""
List = []
count = 0

count = count+1

if count==1:
return None
if List[-n].next==None:
List[-n-1].next = None
return List[0]
else:
List[-n].val = List[-n].next.val
List[-n].next = List[-n].next.next
return List[0]

my_solution = Solution()
print(a)
'''

'''

class Solution:
"""
:type n: int
:rtype: ListNode
"""
dummy = ListNode(0) #初始节点 报错 NameError: name 'ListNode' is not defined
p1 = p2 = dummy
for i in range(n):
p1 = p1.next
while p1.next:
p1 = p1.next
p2 = p2.next
p2.next = p2.next.next
return dummy.next

my_solution = Solution()
print(a)

'''
3

'''
'''

'''

# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
"""
:rtype: ListNode
"""
return []
last_temp = None
while(temp!=None):
temp_next = temp.next  #AttributeError: 'list' object has no attribute 'next'

temp.next = last_temp
last_temp = temp
temp = temp_next

return last_temp
my_solution = Solution()
print(a)

'''

'''
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
"""
:rtype: ListNode
"""
while curr:
temp=curr.next#暂存curr的下一个地址
curr.next=newList#curr.next指向这个新链表，相当于断开curr与后面的连接
newList=curr#将当前节点赋给新链表
curr=temp#暂存的curr.next赋给curr
return newList

'''

'''

class ListNode:
def __init__(self, x):
self.val = x
self.next = None

class Solution:
# @return {ListNode}
while cur != None:
pre.next = cur.next
cur = pre.next

s = Solution()

for i in range(1, 10):
node = ListNode(i)
cur.next = node
cur = node

'''
4

'''

#方法1 ：递归
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1==None and l2==None:
return None
if l1==None:
return l2
if l2==None:
return l1
if l1.val<=l2.val:
l1.next=self.mergeTwoLists(l1.next,l2)
return l1
else:
l2.next=self.mergeTwoLists(l1,l2.next)
return l2

#方法2：非递归
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
while l1!=None and l2!=None:
if l1.val <= l2.val:
l1 = l1.next
else:
l2 = l2.next
if l1 != None:
elif l2 != None:
return first.next

#方法3

class ListNode:

def __init__(self, x):

self.val = x

self.next = None

class Solution:

def mergeTwoLists(self, l1, l2):

"""

:type l1: ListNode

:type l2: ListNode

:rtype: ListNode

"""

while l1 and l2:

if l1.val<l2.val:

pre.next = l1

l1 = l1.next

else:

pre.next = l2

l2 = l2.next

pre = pre.next

if l1:

pre.next = l1

elif l2:

pre.next = l2

# 有序链表

n1 = ListNode(3)

n2 = ListNode(4)

n3 = ListNode(9)

n1.next = n2

n2.next = n3

# 有序链表

m1 = ListNode(5)

m2 = ListNode(7)

m3 = ListNode(8)

m1.next = m2

m2.next = m3

s = Solution()

while res:

print(res.val)

res = res.next

'''
5

你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题？
'''
#方法1
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
"""
:rtype: bool
"""
#复杂度满足要求，空间复杂度为O(n),不满足进阶要求
return True
array=[]
while cur:
array.append(cur.val)
cur=cur.next
return array==array[::-1]

'''
6

你能否不使用额外空间解决此题？
'''

# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
"""
:rtype: bool
"""
return False
while p2.next and p2.next.next:#判断跑得快的是否为空
p1=p1.next
p2=p2.next.next
if p1==p2:#存在环则必然会出现相等
return True
return False

#方法2：

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution: