Welcome to Subscribe On Youtube
Question
Formatted question description: https://leetcode.ca/all/83.html
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.
Example 1:
Input: head = [1,1,2] Output: [1,2]
Example 2:
Input: head = [1,1,2,3,3] Output: [1,2,3]
Constraints:
- The number of nodes in the list is in the range
[0, 300]
. -100 <= Node.val <= 100
- The list is guaranteed to be sorted in ascending order.
Algorithm
Traverse this linked list, and compare each node with the following nodes. If the value of the node is the same, just skip the next pointer of the previous node with the same value and point to the next node. After traversing in this way, all duplicate nodes will be skipped, and the linked list left will have no duplicates.
Code
-
public class Remove_Duplicates_from_Sorted_List { /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; // ListNode dummy = new ListNode(0); // dummy.next = head; ListNode prev = head; ListNode p = head.next; while (p != null) { if (p.val == prev.val) { prev.next = p.next; p = p.next; } else { prev = p; p = p.next; } } return head; } } } ############ /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode cur = head; while (cur != null && cur.next != null) { if (cur.val == cur.next.val) { cur.next = cur.next.next; } else { cur = cur.next; } } return head; } }
-
# 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: ListNode) -> ListNode: cur = head while cur and cur.next: if cur.val == cur.next.val: cur.next = cur.next.next else: cur = cur.next return head ############ # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def deleteDuplicates(self, head): """ :type head: ListNode :rtype: ListNode """ dummy = ListNode(None) dummy.next = head p = dummy while p and p.next: if p.val == p.next.val: p.next = p.next.next else: p = p.next return dummy.next
-
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* deleteDuplicates(ListNode* head) { ListNode* cur = head; while (cur != nullptr && cur->next != nullptr) { if (cur->val == cur->next->val) { cur->next = cur->next->next; } else { cur = cur->next; } } return head; } };
-
func deleteDuplicates(head *ListNode) *ListNode { current := head for current != nil && current.Next != nil { if current.Val == current.Next.Val { current.Next = current.Next.Next } else { current = current.Next } } return head }
-
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} head * @return {ListNode} */ var deleteDuplicates = function (head) { var p = head; while (p && p.next) { if (p.val == p.next.val) { p.next = p.next.next; } else { p = p.next; } } return head; };
-
public class Solution { public ListNode DeleteDuplicates(ListNode head) { if (head == null) return null; var last = head; var current = head.next; while (current != null) { if (current.val != last.val) { last.next = current; last = current; } else { last.next = null; } current = current.next; } return head; } }