zoukankan      html  css  js  c++  java
  • leetcode83

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     public int val;
     *     public ListNode next;
     *     public ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode DeleteDuplicates(ListNode head)
        {
            //if (head == null || head.next == null)
                //{
                //    return head;
                //}
                //head.next = DeleteDuplicates(head.next);
    
                //if (head.val == head.next.val)
                //{
                //    return head.next;
                //}
                //else
                //{
                //    return head;
                //}
    
                if (head != null)
                {
                    var cur = head;
                    var next = head.next;                
                    while (next != null)
                    {
                        if (cur.val == next.val)
                        {
                            cur.next = next.next;//对原链表的修改                        
                        }
                        else
                        {
                            cur = next;//移动指针                        
                        }
                        next = next.next;//移动指针
                    }
                }
                return head;
        }
    }

    https://leetcode.com/problems/remove-duplicates-from-sorted-list/#/description

    补充一个python的实现:

     1 class Solution:
     2     def deleteDuplicates(self, head: ListNode) -> ListNode:
     3         if head == None:
     4             return None
     5         cur = head
     6         nextnode = head.next
     7         while nextnode != None:
     8             if cur.val == nextnode.val:
     9                 cur.next = nextnode.next
    10             else:
    11                 cur = nextnode
    12             nextnode = nextnode.next
    13         return head
  • 相关阅读:
    实验2实验报告
    实验1实验报告
    汇编实验九
    汇编实验5
    汇编实验四
    汇编实验三
    汇编实验二
    汇编实验一
    汇编第一章
    浅谈webpack4.0 性能优化
  • 原文地址:https://www.cnblogs.com/asenyang/p/6732652.html
Copyright © 2011-2022 走看看