LeetCode-Easy刷题(17) Remove Duplicates from Sorted List
Posted 当以乐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-Easy刷题(17) Remove Duplicates from Sorted List相关的知识,希望对你有一定的参考价值。
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
.
删除排好序链表中重复的数字.返回一个数字不重复的链表
//链表指针控制 双指针
public ListNode deleteDuplicates(ListNode head)
if(head ==null)
return head;
ListNode helper = new ListNode(0);
ListNode pre = helper;
pre.next = head;
while(head.next!=null)
ListNode next = head.next;
if(head.val != next.val)
pre.next.next = next;
pre = pre.next;
head = next;
if(pre.next.next!=null)
pre.next.next=null;;
return helper.next;
开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系
以上是关于LeetCode-Easy刷题(17) Remove Duplicates from Sorted List的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode-Easy刷题(31) Single Number