83. Remove Duplicates from Sorted List(Linked List-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.

题意:对给定的排好序的链表,删除重复的元素,只留下出现一次的元素

思路:当元素和下一个元素比对,如果相同,当前元素的next指针指向下一个元素的next指针。

Language : c

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    struct ListNode* cur = (int *)malloc(sizeof(struct ListNode));
    cur = head;
    while(cur != NULL){
        while(cur->next != NULL && cur->val == cur->next->val){
            cur->next = cur->next->next;
        }
        cur = cur->next;
    }
    return head;
}

Language : cpp

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* cur = head;
        while(cur != NULL){
            while(cur->next != NULL && cur->val == cur->next->val){
                cur->next = cur->next->next;
            }
            cur = cur->next;
        }
        return head;
    }
};

Language : python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """ 
        now = head
        while head:
            while head.next and head.val == head.next.val:
                head.next = head.next.next

            head = head.next
        return now

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏计算机视觉与深度学习基础

Leetcode 61 Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative...

20910
来自专栏LeetCode

LeetCode 206. Reverse Linked List

1042
来自专栏desperate633

LeetCode 25. Reverse Nodes in k-Group分析代码

我们设计一个翻转k的算法 head -> n1 -> n2 ... nk -> nk+1 => head -> nk -> nk-1 .. n1 -> nk...

1062
来自专栏计算机视觉与深度学习基础

Leetcode 2 Add Two Numbers

题目没有说清楚,trick挺多,考察细心程度的水题吧。 You are given two linked lists representing two ...

2106
来自专栏desperate633

LintCode 链表划分题目代码

样例 给定链表 1->4->3->2->5->2->null,并且 x=3 返回** 1->2->2->4->3->5->null**

722
来自专栏Jack-Cui

21. Merge Two Sorted Lists(Linked List-Easy)

Merge two sorted linked lists and return it as a new list. The new list should b...

19110
来自专栏计算机视觉与深度学习基础

Leetcode 21 Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should b...

2099
来自专栏书山有路勤为径

链表逆序

1.构造5个节点a,b,c,d,e,并对它们进行初始化; 2.将a,b,c,d,e,5个节点连接在一起

1004
来自专栏Jack-Cui

234. Palindrome Linked List(Linked List-Easy)

Given a singly linked list, determine if it is a palindrome. Follow up: Could yo...

19710
来自专栏一个会写诗的程序员的博客

Android中的WebView之loadDataWithBaseURL()与loadData()Android中的WebView之loadDataWithBaseURL()与loadData()

1221

扫码关注云+社区

领取腾讯云代金券