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.
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; }