首页 > 其他 > 详细

【LeetCode题目记录-8】从排序后的单链表中去除重复元素

时间:2014-09-15 17:48:19      阅读:127      评论:0      收藏:0      [点我收藏+]

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.

【分析-原创】相等的话nextNode后移即可,不相等的话currentNodenextNode都后移。

   

 <span style="font-weight: normal;">/**
     * 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(ListNode head) {
            if(head==null||head.next==null) return head;
            ListNode currentNode=head;
            ListNode nextNode=null;
            while(currentNode.next!=null){
                nextNode=currentNode.next;
                if(currentNode.val==nextNode.val){
                    nextNode=nextNode.next;
                    currentNode.next=nextNode;
                   //currentNode=nextNode;
                }else{
                    currentNode=nextNode;
                    nextNode=nextNode.next;
                }
               
            }
            return head;
           
        }
    }</span>

【LeetCode题目记录-8】从排序后的单链表中去除重复元素

原文:http://blog.csdn.net/brillianteagle/article/details/39295055

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!