DEV Community

anjan-dutta
anjan-dutta

Posted on

Remove duplicates from an unsorted linked list without using any extra space [Javascript]

// defining a linked list 
let LL = {data: 1, next: {data: 2, next: {data: 3, next: {data: 1, next: {data: 2, next: {data: 5, next: {data:1, next: null}}}}}}};

let HEAD = LL;

while(HEAD != null) {
    let HEAD2 = HEAD;
    while( HEAD2.next != null) {
        if(HEAD.data == HEAD2.next.data) {
            HEAD2.next = HEAD2.next.next;
        } else {
            HEAD2 = HEAD2.next;
        }
    }
    HEAD = HEAD.next;
}

console.log(LL);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

SurveyJS custom survey software

JavaScript UI Library for Surveys and Forms

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

View demo

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay