Created
January 13, 2023 21:21
-
-
Save tanvirstreame/edb38b575c99f6548954654d738bd893 to your computer and use it in GitHub Desktop.
Linklistt bubble sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arr = [4,2,1,3]; | |
class Node { | |
constructor(value) { | |
this.value = value; | |
this.next = null; | |
} | |
} | |
let node, temp; | |
for(let i = arr.length - 1; i >= 0; i--) { | |
if(!node) { | |
node = new Node(arr[i]); | |
} | |
else { | |
temp = new Node(arr[i]); | |
temp.next = node; | |
node = temp; | |
} | |
} | |
var sortList = function (head) { | |
let nextHead = head; | |
while(nextHead != null) { | |
if(nextHead?.value > nextHead?.next?.value) { | |
let temp = nextHead?.value; | |
nextHead.value = nextHead.next.value; | |
nextHead.next.value = temp; | |
nextHead = head; | |
} | |
else { | |
nextHead = nextHead.next; | |
} | |
} | |
return head; | |
}; | |
console.log(sortList(node)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment