Remove Duplicates from a Linked List

Beschreibung

Karteikarten am Remove Duplicates from a Linked List, erstellt von Suhas S am 13/02/2018.
Suhas S
Karteikarten von Suhas S, aktualisiert more than 1 year ago
Suhas S
Erstellt von Suhas S vor fast 8 Jahre
0
0

Zusammenfassung der Ressource

Frage Antworten
Remove duplicate nodes from linked list of integers while keeping only the first occurrence of duplicates. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode deleteDuplicates(ListNode head) { if(head == null) return head; ListNode temp = head; while(temp != null ){ if(temp.next == null){ break; } ListNode next = temp.next; if(temp.val == next.val){ temp.next = next.next; }else{ temp = temp.next; } } return head; } }
Zusammenfassung anzeigen Zusammenfassung ausblenden

ähnlicher Inhalt

HW 2_1
Kevin Magno
Reverse Singly linked list
Suhas S