Skip to content

Instantly share code, notes, and snippets.

@tanvirstreame
Created January 13, 2023 21:21
Show Gist options
  • Save tanvirstreame/edb38b575c99f6548954654d738bd893 to your computer and use it in GitHub Desktop.
Save tanvirstreame/edb38b575c99f6548954654d738bd893 to your computer and use it in GitHub Desktop.
Linklistt bubble sort
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