Write a C# Program To Remove Duplicates From a Sorted linked list?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinkedListAlgorithm
{
partial class LinkedListStack
{
//Remove duplicates from a sorted linked list (Assumes nodes contain integers as data)
//Source: https://leetcode.com/problems/remove-duplicates-from-sorted-list/
public void RemoveDuplicates()
{
Node lag = head;
Node lead = head.Next;
while (lead != null)
{
if ((int)lag.Data == (int)lead.Data)
{
lag.Next = lead.Next;
lead = lag.Next;
}
else
{
lag = lag.Next;
lead = lead.Next;
}
}
}
//LinkedListStack list = new LinkedListStack();
//list.Push(1);
//list.Push(1);
//list.Push(2);
//list.Push(2);
//list.Push(3);
//list.Push(3);
//list.RemoveDuplicates();
//Console.WriteLine(list.Pop());
//Console.WriteLine(list.Pop());
//Console.WriteLine(list.Pop());
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinkedListAlgorithm
{
partial class LinkedListStack
{
//Remove duplicates from a sorted linked list (Assumes nodes contain integers as data)
//Source: https://leetcode.com/problems/remove-duplicates-from-sorted-list/
public void RemoveDuplicates()
{
Node lag = head;
Node lead = head.Next;
while (lead != null)
{
if ((int)lag.Data == (int)lead.Data)
{
lag.Next = lead.Next;
lead = lag.Next;
}
else
{
lag = lag.Next;
lead = lead.Next;
}
}
}
//LinkedListStack list = new LinkedListStack();
//list.Push(1);
//list.Push(1);
//list.Push(2);
//list.Push(2);
//list.Push(3);
//list.Push(3);
//list.RemoveDuplicates();
//Console.WriteLine(list.Pop());
//Console.WriteLine(list.Pop());
//Console.WriteLine(list.Pop());
}
}
Post a Comment