Skip to content

Commit 949f41a

Browse files
solves intersection of 2linked lists in python
1 parent 36f6504 commit 949f41a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LinkedListCycle.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/linked_list_cycle.py) |
4646
| 155 | [Min Stack](https://leetcode.com/problems/min-stack) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MinStack.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/min_stack.py) |
4747
| 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4) | Easy | |
48-
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/IntersectionOf2LinkedLists.java) |
48+
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/IntersectionOf2LinkedLists.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/intersecction_of_two_linked_lists.py) |
4949
| 167 | [Two Sum II - Input Array is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/TwoSumIIInputArrayIsSorted.java) |
5050
| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ExcelSheetColumnTitle.java) |
5151
| 169 | [Majority Element](https://leetcode.com/problems/majority-element) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MajorityElement.java) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class ListNode:
2+
def __init__(self, x):
3+
self.val = x
4+
self.next = None
5+
6+
7+
class Solution:
8+
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
9+
runner_1, runner_2 = headA, headB
10+
11+
while runner_1 is not runner_2:
12+
if runner_1 is None:
13+
runner_1 = headB
14+
else:
15+
runner_1 = runner_1.next
16+
17+
if runner_2 is None:
18+
runner_2 = headA
19+
else:
20+
runner_2 = runner_2.next
21+
22+
return None if runner_1 is None else runner_1

0 commit comments

Comments
 (0)