File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
src/removeDuplicatesFromSortedList Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * public class ListNode {
4+ * int val;
5+ * ListNode next;
6+ * ListNode(int x) {
7+ * val = x;
8+ * next = null;
9+ * }
10+ * }
11+ */
12+ public class Solution {
13+ public ListNode deleteDuplicates (ListNode head ) {
14+ if (head == null || head .next == null ) { return head ; }
15+ ListNode fakeHead = new ListNode (0 );
16+ fakeHead .next = head ;
17+ ListNode b = fakeHead ;
18+ ListNode f = head ;
19+ boolean isDuplicated = false ;
20+ int val = 0 ;
21+ while (f != null ) {
22+ if (isDuplicated ) {
23+ if (f .val != val ) {
24+ isDuplicated = false ;
25+ b .next = f ;
26+ }
27+ }
28+ if (!isDuplicated && f .next != null ) {
29+ if (f .next .val != f .val ) {
30+ b = f ;
31+ } else {
32+ isDuplicated = true ;
33+ val = f .val ;
34+ }
35+ }
36+ f = f .next ;
37+ }
38+ if (isDuplicated ) {
39+ b .next = null ;
40+ }
41+ return fakeHead .next ;
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments