forked from leetcoders/LeetCode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicatesfromSortedList.java
More file actions
executable file
·55 lines (53 loc) · 1.54 KB
/
Copy pathRemoveDuplicatesfromSortedList.java
File metadata and controls
executable file
·55 lines (53 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Author: King, wangjingui@outlook.com
Date: Dec 12, 2014
Problem: Remove Duplicates from Sorted List
Difficulty: Easy
Source: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/
Notes:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
Solution: 1. Delete duplicates directly.
2. Copy value first (like Remove Duplicates from Array) and then delete the remaining elements.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates_1(ListNode head) {
if(head == null || head.next == null) return head;
ListNode pre = head, cur = head.next;
while(cur != null) {
if(pre.val == cur.val) {
pre.next = cur.next;
} else {
pre = pre.next;
}
cur = cur.next;
}
return head;
}
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null) return head;
ListNode pre = head, cur = head.next;
while(cur != null) {
if (pre.val != cur.val) {
pre.next.val = cur.val;
pre = pre.next;
}
cur = cur.next;
}
pre.next = null;
return head;
}
}