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的实现:

 class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if head == None:
return None
cur = head
nextnode = head.next
while nextnode != None:
if cur.val == nextnode.val:
cur.next = nextnode.next
else:
cur = nextnode
nextnode = nextnode.next
return head
上一篇:hive Spark SQL分析窗口函数


下一篇:go,函数作为参数类型