Skip to content

Commit ae915df

Browse files
Create 876_1_Middle_of_Linked_List.py
Brute force approach
1 parent 915e553 commit ae915df

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

876_1_Middle_of_Linked_List.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
""""
2+
876. Middle of the Linked List
3+
4+
Given the head of a singly linked list, return the middle node of the linked list.
5+
6+
If there are two middle nodes, return the second middle node."""
7+
8+
#Brute-force approach-1
9+
10+
11+
# Definition for singly-linked list.
12+
# class ListNode:
13+
# def __init__(self, val=0, next=None):
14+
# self.val = val
15+
# self.next = next
16+
class Solution:
17+
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
18+
curr = head
19+
count = 1
20+
21+
while curr.next:
22+
curr = curr.next
23+
count+=1
24+
25+
curr = head
26+
if count%2 == 0:
27+
for count in range(count,(count//2),-1):
28+
curr = curr.next
29+
30+
31+
else:
32+
for count in range(count,(count//2)+1,-1):
33+
curr = curr.next
34+
35+
36+
head = curr
37+
return head
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+

0 commit comments

Comments
 (0)