-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path876_middle_of_the_linked_list.py
67 lines (52 loc) · 1.6 KB
/
876_middle_of_the_linked_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
---
title: Middle of the linked list
number: 876
difficulty: easy
tags: ['Linked list','Two pointers']
solved: true
---
"""
"""
Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.
"""
from typing import Optional
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def __repr__(self):
### Lets and __repr__ method so its easier to debug
return f"ListNode(val={self.val},next={self.next})"
def list_to_ListNode(array)->ListNode:
if len(array)==0:return None
if len(array)==1:
return ListNode(array[0])
return ListNode(array[0],next=list_to_ListNode(array[1:]))
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
return self.two_pointers(head)
def simple(self,list: ListNode):
## transfer to array
## WORKS but lets try different method,maybe two pointers, fast and slow
result = []
iter = list
while iter:
result.append(iter.val)
iter = iter.next
return list_to_ListNode(result[len(result)//2:])
def two_pointers(self,list:ListNode):
slow = list
fast = list
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
if __name__ == '__main__':
list = [1,2,3,4,5]
list1 = [1,2,3,4,5,6]
l1 = list_to_ListNode(list)
l2 = list_to_ListNode(list1)
print(Solution().middleNode(l2))