每日一题之LeetCode237删除链表中的节点876链表的中间节点
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每日一题之LeetCode237删除链表中的节点876链表的中间节点相关的知识,希望对你有一定的参考价值。
237
class ListNode:
def init(self, x):
self.val = x
self.next = None
class Solution:
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
head1 = ListNode(4)
n1 = ListNode(5)
n2 = ListNode(1)
n3 = ListNode(9)
head1.next = n1
n1.next = n2
n2.next = n3
t=Solution()
t.deleteNode(n1)
print(head1.next.val)
876
-- coding: utf-8 --
"""
Created on Thu Nov 22 21:45:28 2018
@author: Administrator
"""
class ListNode:
def init(self, x):
self.val = x
self.next = None
class Solution(object):
def middleNode(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
flag=True
i=0
t=head
while flag:
if t.next!=None:
i=i+1
t=t.next
else:
flag=False
i=i+1
j=int(i/2)+1
while k:
head=head.next
n=n+1
if n=j:
k=False
for m in range(0,j-1):
head=head.next
return head
head1 = ListNode(1)
n1 = ListNode(2)
n2 = ListNode(3)
n3 = ListNode(4)
n4 = ListNode(5)
head1.next = n1
n1.next = n2
n2.next = n3
n3.next = n4
t=Solution()
print(t.middleNode(head1).val)
#print(head1.next.val)
以上是关于每日一题之LeetCode237删除链表中的节点876链表的中间节点的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 575. 分糖果 / 237. 删除链表中的节点 / 407. 接雨水 II
《LeetCode之每日一题》:99.删除排序链表中的重复元素 II
《LeetCode之每日一题》:98.删除排序链表中的重复元素