LeetCode删链表节点系列总结LeetCode 83. Remove Duplicates from Sorted List;

83. Remove Duplicates from Sorted List

  • Total Accepted: 175237
  • Total Submissions: 444457
  • Difficulty: Easy
  • Contributor: LeetCode

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.

Subscribe to see which companies asked this question.

public ListNode deleteDuplicates(ListNode head) {
    if(head==null) return head;
    ListNode pre=head;
    ListNode cur=head.next;
    while(cur!=null){
        if(cur.val==pre.val){
            pre.next=cur.next;
            cur=cur.next;
            //不用修改pre
        }else{
            pre=pre.next;
            cur=cur.next;
        }
    }
    return head;
}

82. Remove Duplicates from Sorted List II

  • Total Accepted: 104206
  • Total Submissions: 358219
  • Difficulty: Medium
  • Contributor: LeetCode

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

Subscribe to see which companies asked this question.

Show Tags







public ListNode deleteDuplicates(ListNode head) {
    ListNode helper = new ListNode(0);
    ListNode pre = helper;
    boolean isDup = false;
    while (head != null && head.next != null) {
        if (head.val == head.next.val) {
            isDup = true;
        } else if (isDup ) {
            isDup = false;
        } else {
            pre.next = head;
            pre = pre.next;
        }
        head = head.next;
    }
    if (isDup) {
        pre.next = null;
    } else {
        pre.next = head;
    }
    return helper.next;
}

19. Remove Nth Node From End of List

  • Total Accepted: 170940
  • Total Submissions: 520559
  • Difficulty: Medium
  • Contributor: LeetCode

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

题解:首先先让faster从起始点往后走n步。

然后再让slower和faster一起走,直到faster.next==null时候,slower所指向的node就是需要删除的节点的前一个节点。

public ListNode removeNthFromEnd(ListNode head, int n){
    ListNode fast=head;
    ListNode slow=head;
    while(n>0){
        fast=fast.next;
        n=n-1;
    }
    //要删除的是头结点
    if(fast==null){
        head=head.next;
        return head;
    }
    //没有用fast!=null,使得slow取代了pre的作用
    while (fast.next!=null){
        slow=slow.next;
        fast=fast.next;
    }

    slow.next=slow.next.next;
    return head;
}

203. Remove Linked List Elements

  • Total Accepted: 108962
  • Total Submissions: 342991
  • Difficulty: Easy
  • Contributor: LeetCode

Remove all elements from a linked list of integers that have value val.

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.

public ListNode removeElementsI(ListNode head, int val) {
    if(head==null) return head;
    head.next=removeElementsI(head.next, val);

    return head.val==val?head.next:head;
}
public ListNode removeElements(ListNode head, int val) {
    if(head==null) return head;
    ListNode pre=new ListNode(0);
    pre.next=head;
    ListNode res= pre;
    while(head!=null){
        if(head.val==val){
            pre.next=head.next;
        }else {
            pre=pre.next;
        }
        head=head.next;
    }
    return res.next;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值