Skip to content

Commit

Permalink
添加(面试题02.07.链表相交.md):增加typescript版本
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaofei-2020 committed Jan 12, 2022
1 parent 6de5da7 commit ba8e46f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions problems/面试题02.07.链表相交.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,43 @@ var getIntersectionNode = function(headA, headB) {
};
```

TypeScript:

```typescript
function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
let sizeA: number = 0,
sizeB: number = 0;
let curA: ListNode | null = headA,
curB: ListNode | null = headB;
while (curA) {
sizeA++;
curA = curA.next;
}
while (curB) {
sizeB++;
curB = curB.next;
}
curA = headA;
curB = headB;
if (sizeA < sizeB) {
[sizeA, sizeB] = [sizeB, sizeA];
[curA, curB] = [curB, curA];
}
let gap = sizeA - sizeB;
while (gap-- && curA) {
curA = curA.next;
}
while (curA && curB) {
if (curA === curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
};
```

C:

```c
Expand Down

0 comments on commit ba8e46f

Please sign in to comment.