Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions reverse-linked-list/soobing.ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers
  • 설명: 이 코드는 두 포인터(prev, current)를 활용하여 링크드 리스트를 역순으로 뒤집는 과정에서 서로를 참조하며 이동하는 방식으로 구현되어 있습니다.

Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,28 @@ interface ListNode {
next: ListNode | null;
}

// function reverseList(head: ListNode | null): ListNode | null {
// let prev: ListNode | null = null;

// while (head) {
// const next = head.next;
// head.next = prev;
// prev = head;
// head = next;
// }

// return prev;
// }

// 3rd tried
function reverseList(head: ListNode | null): ListNode | null {
let prev: ListNode | null = null;

while (head) {
const next = head.next;
head.next = prev;
prev = head;
head = next;
let current = head;
while(current) {
const next = current?.next ?? null;
current.next = prev;
prev = current;
current = next;
}

return prev;
}
};
Loading