Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2cae197

Browse files
author
applewjg
committed
RemoveDuplicatesfromSortedList I && II
1 parente93c329 commit2cae197

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

‎RemoveDuplicatesfromSortedList.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 12, 2014
4+
Problem: Remove Duplicates from Sorted List
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
7+
8+
Notes:
9+
Given a sorted linked list, delete all duplicates such that each element appear only once.
10+
For example,
11+
Given 1->1->2, return 1->2.
12+
Given 1->1->2->3->3, return 1->2->3.
13+
14+
Solution: 1. Delete duplicates directly.
15+
2. Copy value first (like Remove Duplicates from Array) and then delete the remaining elements.
16+
*/
17+
/**
18+
* Definition for singly-linked list.
19+
* public class ListNode {
20+
* int val;
21+
* ListNode next;
22+
* ListNode(int x) {
23+
* val = x;
24+
* next = null;
25+
* }
26+
* }
27+
*/
28+
publicclassSolution {
29+
publicListNodedeleteDuplicates_1(ListNodehead) {
30+
if(head ==null ||head.next ==null)returnhead;
31+
ListNodepre =head,cur =head.next;
32+
while(cur !=null) {
33+
if(pre.val ==cur.val) {
34+
pre.next =cur.next;
35+
}else {
36+
pre =cur;
37+
}
38+
cur =cur.next;
39+
}
40+
returnhead;
41+
}
42+
publicListNodedeleteDuplicates(ListNodehead) {
43+
if(head ==null ||head.next ==null)returnhead;
44+
ListNodepre =head,cur =head.next;
45+
while(cur !=null) {
46+
if (pre.val !=cur.val) {
47+
pre.next.val =cur.val;
48+
pre =pre.next;
49+
}
50+
cur =cur.next;
51+
}
52+
pre.next =null;
53+
returnhead;
54+
}
55+
}

‎RemoveDuplicatesfromSortedListII.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 12, 2014
4+
Problem: Remove Duplicates from Sorted List II
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
7+
Notes:
8+
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
9+
For example,
10+
Given 1->2->3->3->4->4->5, return 1->2->5.
11+
Given 1->1->1->2->3, return 2->3.
12+
13+
Solution: 1. iterative 2. recursive
14+
*/
15+
/**
16+
* Definition for singly-linked list.
17+
* public class ListNode {
18+
* int val;
19+
* ListNode next;
20+
* ListNode(int x) {
21+
* val = x;
22+
* next = null;
23+
* }
24+
* }
25+
*/
26+
publicclassSolution {
27+
publicListNodedeleteDuplicates_1(ListNodehead) {
28+
ListNodedummy =newListNode(-1);
29+
ListNodecur =dummy;
30+
while (head !=null) {
31+
if (head.next !=null &&head.val ==head.next.val) {
32+
while (head.next !=null &&head.val ==head.next.val)
33+
head =head.next;
34+
}else {
35+
cur.next =head;
36+
cur =cur.next;
37+
}
38+
head =head.next;
39+
}
40+
cur.next =null;
41+
returndummy.next;
42+
}
43+
publicListNodedeleteDuplicates(ListNodehead) {
44+
if (head ==null)returnnull;
45+
if (head.next ==null ||head.val !=head.next.val){
46+
head.next =deleteDuplicates(head.next);
47+
returnhead;
48+
}
49+
while (head.next !=null &&head.val ==head.next.val)
50+
head =head.next;
51+
returndeleteDuplicates(head.next);
52+
}
53+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp