Leetcode刷题记——83. Remove Duplicates from Sorted List(删除有序链表的重复结点)
2017-02-28 15:06
369 查看
一、题目叙述:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
Subscribe to see which companies asked this question.
二、解题思路:
Easy题,删除有序链表里的重复结点,因为链表本身有序所以很简单。
思路:
(1)创建一个指针指向头结点,查看其后一结点是否与其值相等,若相等,删掉其后结点,否则,移动指针向后遍历。
(2)注意头结点为空的情况。
三、源码:
public class Solution
{
public ListNode deleteDuplicates(ListNode head)
{
ListNode tem = head;
if (head == null) return head;
while (tem.next != null)
{
if (tem.next.val == tem.val)
tem.next = tem.next.next;
else
tem = tem.next;
}
return head;
}
public static void main(String args[])
{
ListNode a = new ListNode(1);
ListNode first = a;
a.next = new ListNode(1);
a = a.next;
a.next = new ListNode(2);
//String a = "";
//String b = "";
// int[] digits = {0};
Solution solution = new Solution();
// int[][] abc = {{2}};
// int[] b = {2,3,4};
// for(int i = 0; i < abc.length; i ++)
ListNode res = solution.deleteDuplicates(first);
while (res != null)
{
System.out.print(res.val);
res = res.next;
}
}
}
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.
Subscribe to see which companies asked this question.
二、解题思路:
Easy题,删除有序链表里的重复结点,因为链表本身有序所以很简单。
思路:
(1)创建一个指针指向头结点,查看其后一结点是否与其值相等,若相等,删掉其后结点,否则,移动指针向后遍历。
(2)注意头结点为空的情况。
三、源码:
public class Solution
{
public ListNode deleteDuplicates(ListNode head)
{
ListNode tem = head;
if (head == null) return head;
while (tem.next != null)
{
if (tem.next.val == tem.val)
tem.next = tem.next.next;
else
tem = tem.next;
}
return head;
}
public static void main(String args[])
{
ListNode a = new ListNode(1);
ListNode first = a;
a.next = new ListNode(1);
a = a.next;
a.next = new ListNode(2);
//String a = "";
//String b = "";
// int[] digits = {0};
Solution solution = new Solution();
// int[][] abc = {{2}};
// int[] b = {2,3,4};
// for(int i = 0; i < abc.length; i ++)
ListNode res = solution.deleteDuplicates(first);
while (res != null)
{
System.out.print(res.val);
res = res.next;
}
}
}
相关文章推荐
- LeetCode | Remove Duplicates from Sorted List II(删除链表中重复结点2)
- leetcode 删除有序链表中的重复元素
- [LeetCode]Merge Two Sorted Lists & Remove Duplicates from Sorted List 两个有序链表合并为一个&删除链表重复元素
- leetcode-83. Remove Duplicates from Sorted List(删除有序链表中的重复元素)
- 【LeetCode-面试算法经典-Java实现】【083-Remove Duplicates from Sorted List(排序的单链表中删除重复的结点)】
- 单链表的创建和遍历、求单链表中节点的个数、查找单链表中的中间结点、判断单链表是否有环、取出有环链表中环的长度,删除有序链表中的重复结点
- [Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点
- [LeetCode]83. Remove Duplicates from Sorted List(删除有序链表的重复元素 )
- 删除有序链表中的重复结点
- LeetCode基础-链表-删除重复结点
- leetcode83,删除有序链表中的重复元素
- LeetCode Remove Duplicates from Sorted List 删除有序链表中的重复结点
- 剑指offer--删除链表中重复的结点
- Leetcode Remove Duplicates from Sorted List 删除链表中的重复元素
- 删除有序链表中重复的元素
- LeetCode OJ 之 Remove Duplicates from Sorted List(删除有序链表中重复的数)
- 链表(三)——链表删除冗余结点&插入结点到有序链表
- Leetcode Remove Duplicates from Sorted List II 删除链表中重复的元素II
- [2178]链表的有序集合 (链表的插排法+重复元素的删除) SDUT
- 有序链表删除重复元素