Skip to content

Commit f753328

Browse files
authored
feat: add typescript solution to lc problem: No.0160.Intersection of Two Linked Lists (doocs#440)
1 parent 08a4091 commit f753328

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

solution/0100-0199/0160.Intersection of Two Linked Lists/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,31 @@ public:
200200
}
201201
```
202202

203+
### **TypeScript**
204+
205+
```ts
206+
/**
207+
* Definition for singly-linked list.
208+
* class ListNode {
209+
* val: number
210+
* next: ListNode | null
211+
* constructor(val?: number, next?: ListNode | null) {
212+
* this.val = (val===undefined ? 0 : val)
213+
* this.next = (next===undefined ? null : next)
214+
* }
215+
* }
216+
*/
217+
218+
function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
219+
let p1: ListNode | null = headA;
220+
let p2: ListNode | null = headB;
221+
while (p1 != p2) {
222+
p1 = p1 == null ? headB : p1.next;
223+
p2 = p2 == null ? headA : p2.next;
224+
}
225+
return p1;
226+
};
227+
```
203228
### **...**
204229

205230
```

solution/0100-0199/0160.Intersection of Two Linked Lists/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,31 @@ public:
185185
}
186186
```
187187

188+
### **TypeScript**
189+
190+
```ts
191+
/**
192+
* Definition for singly-linked list.
193+
* class ListNode {
194+
* val: number
195+
* next: ListNode | null
196+
* constructor(val?: number, next?: ListNode | null) {
197+
* this.val = (val===undefined ? 0 : val)
198+
* this.next = (next===undefined ? null : next)
199+
* }
200+
* }
201+
*/
202+
203+
function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
204+
let p1: ListNode | null = headA;
205+
let p2: ListNode | null = headB;
206+
while (p1 != p2) {
207+
p1 = p1 == null ? headB : p1.next;
208+
p2 = p2 == null ? headA : p2.next;
209+
}
210+
return p1;
211+
};
212+
```
188213
### **...**
189214

190215
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
14+
let p1: ListNode | null = headA;
15+
let p2: ListNode | null = headB;
16+
while (p1 != p2) {
17+
p1 = p1 == null ? headB : p1.next;
18+
p2 = p2 == null ? headA : p2.next;
19+
}
20+
return p1;
21+
};

0 commit comments

Comments
 (0)