File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed
Leetcode/leetcodeTags/LinkedList Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ package LinkedList ;
2+
3+ public class RemoveDuplicatesFromSortedList83 {
4+
5+ public class ListNode {
6+ int val ;
7+ ListNode next ;
8+
9+ ListNode () {
10+ }
11+
12+ ListNode (int val ) {
13+ this .val = val ;
14+ }
15+
16+ ListNode (int val , ListNode next ) {
17+ this .val = val ;
18+ this .next = next ;
19+ }
20+ }
21+
22+ public ListNode deleteDuplicates (ListNode head ) {
23+
24+ if (head == null || head .next == null )
25+ return head ;
26+
27+ ListNode recursionHead = deleteDuplicates (head .next );
28+
29+ if (head .val == recursionHead .val )
30+ return recursionHead ;
31+ else {
32+ head .next = recursionHead ;
33+ return head ;
34+ }
35+ }
36+
37+ public ListNode deleteDuplicatesIterative (ListNode head ) {
38+
39+ if (head == null || head .next == null )
40+ return head ;
41+
42+ ListNode temp = head ;
43+
44+ while (temp != null ) {
45+
46+ if (temp .next != null && temp .val == temp .next .val ) {
47+
48+ ListNode nodeToBeDeleted = temp .next ;
49+ temp .next = nodeToBeDeleted .next ;
50+
51+ } else {
52+ temp = temp .next ;
53+ }
54+ }
55+
56+ return head ;
57+ }
58+ }
You can’t perform that action at this time.
0 commit comments