Skip to content

Commit 2fda23a

Browse files
committed
feat: add middle-of-the-linked-list
1 parent 0ce1cfd commit 2fda23a

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

problems/0021-merge-two-sorted-lists/solution.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) ->
1313
p1 = list1
1414
p2 = list2
1515

16-
while p1 is not None and p2 is not None:
16+
while p1 and p2:
1717
if p1.val < p2.val:
1818
p.next = p1
1919
p1 = p1.next
@@ -22,10 +22,10 @@ def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) ->
2222
p2 = p2.next
2323
p = p.next
2424

25-
if p1 is not None:
25+
if p1:
2626
p.next = p1
2727

28-
if p2 is not None:
28+
if p2:
2929
p.next = p2
3030

3131
return dummy.next
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Problem 0876: Middle Of The Linked List
2+
3+
## Link
4+
https://leetcode.com/problems/middle-of-the-linked-list/
5+
6+
## Intuition
7+
Use two pointers moving at different speeds to find the middle efficiently in a single pass.
8+
9+
## Approach
10+
Initialize fast and slow pointers at the head; move fast by two steps and slow by one until fast reaches the end, then return slow.
11+
12+
## Complexity
13+
- Time complexity: O(n), as each node is visited at most once.
14+
- Space complexity: O(1), using only two pointers regardless of list size.
15+
16+
## Notes
17+
Alternate: count nodes then index to middle; for even length, returns second middle; handles empty/one-node lists gracefully.
18+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import *
2+
3+
class ListNode:
4+
def __init__(self, val=0, next=None):
5+
self.val = val
6+
self.next = next
7+
8+
class Solution:
9+
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
10+
fast, slow = head, head
11+
12+
while fast and fast.next:
13+
slow = slow.next
14+
fast = fast.next.next
15+
16+
return slow
17+
18+
if __name__ == "__main__":
19+
def build_list(arr):
20+
dummy = ListNode()
21+
curr = dummy
22+
for v in arr:
23+
curr.next = ListNode(v)
24+
curr = curr.next
25+
return dummy.next
26+
27+
def to_list(node):
28+
res = []
29+
while node:
30+
res.append(node.val)
31+
node = node.next
32+
return res
33+
34+
s = Solution()
35+
list1 = build_list([1,2,3,4,5])
36+
list2 = build_list([1,2,3,4,5,6])
37+
answer_one = s.middleNode(list1)
38+
answer_two = s.middleNode(list2)
39+
print(to_list(answer_one)) # Expected: [3,4,5]
40+
print(to_list(answer_two)) # Expected: [4,5,6]

problems/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
| 0021 | Merge Two Sorted Lists | [0021-merge-two-sorted-lists](0021-merge-two-sorted-lists) |
77
| 0217 | Contains Duplicate | [0217-contains-duplicate](0217-contains-duplicate) |
88
| 0707 | Design Linked List | [0707-design-linked-list](0707-design-linked-list) |
9+
| 0876 | Middle Of The Linked List | [0876-middle-of-the-linked-list](0876-middle-of-the-linked-list) |

0 commit comments

Comments
 (0)