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 <= 100The list is guaranteed to be sorted in ascending order.
Question
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 <= 100The list is guaranteed to be sorted in ascending order.
Solution
Sure, here is a Python solution for the problem:
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def deleteDuplicates(head):
current = head
while current is not None and current.next is not None:
if current.next.val == current.val:
current.next = current.next.next
else:
current = current.next
return head
Here is a step by step explanation of the solution:
-
Define a class
ListNodeto represent each node in the linked list. Each node has a valuevaland a pointernextto the next node. -
Define a function
deleteDuplicatesto remove duplicates from the linked list. The function takes the head of the linked list as input. -
Inside the function, initialize a variable
currentto the head of the linked list. -
Start a while loop that continues as long as
currentis notNoneandcurrent.nextis notNone. This is to ensure that we don't accessNonevalues. -
Inside the loop, check if the value of the next node is the same as the value of the current node. If it is, set the next node of the current node to the node after the next node, effectively skipping over the duplicate node.
-
If the value of the next node is not the same as the value of the current node, move
currentto the next node. -
After the
Similar Questions
Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list, and return the head of the modified linked list.
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) Example 1:Input: head = [1,2,3,4]Output: [2,1,4,3]Example 2:Input: head = []Output: []Example 3:Input: head = [1]Output: [1] Constraints:The number of nodes in the list is in the range [0, 100].0 <= Node.val <= 100
Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.
Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.After doing so, return the head of the final linked list. You may return any such answer.
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.