首页 > 其他 > 详细

每日一题之LeetCode237删除链表中的节点876链表的中间节点

时间:2018-11-22 23:01:49      阅读:165      评论:0      收藏:0      [点我收藏+]
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链表的中间节点

原文:http://blog.51cto.com/13930723/2320836

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!