Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7557727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:12:28+00:00 2026-05-30T12:12:28+00:00

I want to remove duplicates from sorted linked list {0 1 2 2 3

  • 0

I want to remove duplicates from sorted linked list {0 1 2 2 3 3 4 5}.

`

public Node removeDuplicates(Node header)
{
    Node tempHeader = null;
    if(header != null) 
        tempHeader = header.next;
    else return header;
    Node prev = header;
    if((tempHeader == null)) return header ;

    while(tempHeader != null)
    {
        if(tempHeader.data != prev.data)
        {
            prev.setNext(tempHeader);
        }
    tempHeader = tempHeader.next;
    }
    prev = header;
    printList(prev);
    return tempHeader;
}

`

prev.setNext(tempHeader) is not working correctly inside the while loop. Ideally when prev = 2 and tempHeader = 3, prev.next should be node with data = 3.

Printlist function just takes header pointer and prints the list.

Node definition is given below.

public class Node
{
    int data;
    Node next;

    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:12:30+00:00Added an answer on May 30, 2026 at 12:12 pm

    The loop is sorted, so you know that duplicates are going to sit next to each other. If you want to edit the list in place then, you’ve got to have two list pointers (which you do). The one you call tempHeader and prev, and you’ve got to advance them both in the the list as you go (which I don’t see in the code). Otherwise, if you don’t advance the prev pointer as you go, then you’re always comparing the element under tempHeader to the first item in the list, which is not correct.

    An easier way to do this, however, is to build a new list as you go. Simply remember the value of the last item that you appended to the list. Then if the one that you’re about to insert is the same then simply don’t insert it, and when you’re done, just return your new list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to remove duplicates from this list: List<Dictionary<string, object>> val = new List<Dictionary<string,
I want to remove duplicates from a list but what I am doing is
I have a situation where I want to remove duplicates from a collection (list)
I want to remove duplicates from list, without changing order of unique elements in
Possible Duplicate: Remove duplicates from array I have a list of items. I want
I want to remove duplicates from my DataTable so I'm using DataTable.AsEnumerable().Distinct(DataRowComparer.Default) but it
Given a list of strings, I want to sort it alphabetically and remove duplicates.
<ul id=myid> <li>microsoft</li> <li>microsoft</li> <li>apple</li> <li>apple</li> </ul> I want to remove duplicates from li
I simply want to remove duplicates from two lists and combine them into one
Possible Duplicate: Remove duplicates from a List<T> in C# i have a List like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.