leetCode练习(83)
2016-10-17 17:05
399 查看
题目:Remove Duplicates from Sorted List
难度:easy
问题描述:
Given a sorted linked list, delete all duplicates such that each element appear only
once.
For example,
Given
Given
解题思路:
链表新手题···没啥好说的。直接看代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode temp,last;
if(head==null||head.next==null){
return head;
}
temp=head.next;
last=head;
while(true){
if(temp.val==last.val){
last.next=temp.next;
temp=temp.next;
}else{
last=temp;
temp=temp.next;
}
if(temp==null){
break;
}
}
return head;
}
}
难度:easy
问题描述:
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.
解题思路:
链表新手题···没啥好说的。直接看代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode temp,last;
if(head==null||head.next==null){
return head;
}
temp=head.next;
last=head;
while(true){
if(temp.val==last.val){
last.next=temp.next;
temp=temp.next;
}else{
last=temp;
temp=temp.next;
}
if(temp==null){
break;
}
}
return head;
}
}
相关文章推荐
- leetCode练习(41)
- LeetCode Online Judge 题目C# 练习 - Set Matrix Zeroes
- LeetCode Online Judge 题目C# 练习 - Substring with Concatenation of All Words
- LeetCode Online Judge 题目C# 练习 - ZigZag Conversion
- LeetCode Everyday -- 83
- LeetCode Online Judge 题目C# 练习 - Balanced Binary Tree
- leetcode83~Remove Duplicates from Sorted List
- LeetCode练习-求二叉树的深度-Maximu Depth of Binary Tree
- Leetcode练习<十二> 判断整数是否为回文
- leetCode练习(51)
- leetcode || 83、Remove Duplicates from Sorted List
- leetCode练习(56)
- leetCode练习(59)
- leetCode练习(77)
- leetcode练习 Beautiful Arrangement
- leetCode练习(80)
- LeetCode练习 两个排序数组的中位数
- leetCode练习(148)
- leetCode练习(92)
- leetcode83