mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 17:50:39 +08:00
chore: add reverse to singly linked list (#1241)
* Fix: Reverse Singly Linked List * Added reverse in SinglyLinkedList * Added reverse in SinglyLinkedList * Changes made
This commit is contained in:
16
Data-Structures/Linked-List/ReverseSinglyLinkedList.js
Normal file
16
Data-Structures/Linked-List/ReverseSinglyLinkedList.js
Normal file
@ -0,0 +1,16 @@
|
||||
/** A LinkedList based solution to reverse a number
|
||||
Problem Statement: Given a number such that each of its digit is stored in a singly linked list. Reverse the linked list and return the head of the linked list Link for the Problem: https://leetcode.com/problems/reverse-linked-list/ */
|
||||
class ReverseSinglyLinkedList {
|
||||
solution (head) {
|
||||
let prev = null
|
||||
let next = null
|
||||
while (head) {
|
||||
next = head.next
|
||||
head.next = prev
|
||||
prev = head
|
||||
head = next
|
||||
}
|
||||
return prev
|
||||
};
|
||||
}
|
||||
export { ReverseSinglyLinkedList }
|
Reference in New Issue
Block a user