Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
Solution
The third point of constraints is important for solving this problem ➡ the list were sorted in ascending order.
The first solution occurred to me is to use iteration to check the next number is same or not, if same than skip it, if not than add it to a new linked list.
C# Solution
Solution1
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode DeleteDuplicates(ListNode head) {
ListNode resNode = new ListNode();
if(head == null){return head;}
ListNode copyNode = resNode;
copyNode.val = head.val;
while(head!=null)
{
if(copyNode.val!=head.val)
{
ListNode newNode = new ListNode();
newNode.val = head.val;
copyNode.next = newNode;
copyNode = newNode;
}
head = head.next;
}
return resNode;
}
}
Solution2
In our second solution, we get the value of the whole linked list, and save them into a int list.
Then, we use LINQ to distinct the list and remake a new linked list.
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode DeleteDuplicates(ListNode head) {
ListNode resNode = new ListNode();
List<int>valList = new List<int>();
//1.get val list distinct first
if(head == null){return head;}
while(head != null)
{
valList.Add(head.val);
head = head.next;
}
valList = valList.Distinct().OrderBy(x => x).ToList();
//2.remake a linked list and return
resNode.val = valList[0];
ListNode lastNode = resNode;
for(int i =1;i<valList.Count;i++)
{
ListNode nowNode = new ListNode();
nowNode.val = valList[i];
lastNode.next = nowNode;
lastNode = nowNode;
}
return resNode;
}
}
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
resNode = ListNode()
if(head == None):
return head
copyNode = resNode
copyNode.val = head.val
while(head!=None):
if(copyNode.val!=head.val):
newNode = ListNode()
newNode.val = head.val
copyNode.next = newNode
copyNode = newNode
head = head.next
return resNode
Runtime : 44ms、55ms、51ms ≈ Faster than 80~95%
JavaScript Solution
Solution1
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteDuplicates = function(head) {
var resNode = new ListNode();
if(head == null){return head;}
var copyNode = resNode;
copyNode.val = head.val;
while(head!=null)
{
if(copyNode.val!=head.val)
{
var newNode = new ListNode();
newNode.val = head.val;
copyNode.next = newNode;
copyNode = newNode;
}
head = head.next;
}
return resNode;
};
Conclusion
🧡If my solution helps, that is my honor!
✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know (either leave a comment or contact me by Messenger is ok!)
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};