File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -239,6 +239,43 @@ var getIntersectionNode = function(headA, headB) {
239239};
240240```
241241
242+ TypeScript:
243+
244+ ``` typescript
245+ function getIntersectionNode(headA : ListNode | null , headB : ListNode | null ): ListNode | null {
246+ let sizeA: number = 0 ,
247+ sizeB: number = 0 ;
248+ let curA: ListNode | null = headA ,
249+ curB: ListNode | null = headB ;
250+ while (curA ) {
251+ sizeA ++ ;
252+ curA = curA .next ;
253+ }
254+ while (curB ) {
255+ sizeB ++ ;
256+ curB = curB .next ;
257+ }
258+ curA = headA ;
259+ curB = headB ;
260+ if (sizeA < sizeB ) {
261+ [sizeA , sizeB ] = [sizeB , sizeA ];
262+ [curA , curB ] = [curB , curA ];
263+ }
264+ let gap = sizeA - sizeB ;
265+ while (gap -- && curA ) {
266+ curA = curA .next ;
267+ }
268+ while (curA && curB ) {
269+ if (curA === curB ) {
270+ return curA ;
271+ }
272+ curA = curA .next ;
273+ curB = curB .next ;
274+ }
275+ return null ;
276+ };
277+ ```
278+
242279C:
243280
244281``` c
You can’t perform that action at this time.
0 commit comments