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)