-
-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
删除排序链表中的重复元素 #284
Comments
var deleteDuplicates = function(head) {
let temp = head;
while(temp && temp.next){
if(temp.val === temp.next.val){
let next = temp.next
temp.next = next.next
}
else{
temp = temp.next
}
}
return head
}; |
删除排序链表中的重复元素II var deleteDuplicates = function (head) {
let dummyHead = new ListNode(0, head);
let prev = dummyHead;
let cur = head;
while (cur && cur.next) {
if (cur.val === cur.next.val) { //值相同时
let val = cur.val;
while (cur && cur.val === val) { //找到不同值为止
cur = cur.next
}
prev.next = cur; //前指针直接指向重复值之后的节点
}
else {
prev = cur; //注意顺序
cur = cur.next;
}
}
return dummyHead.next;
}; |
删除链表中重复元素II var deleteDuplicates = function(head) {
let ret = new ListNode(null, head), p = ret
if(!head) return head
let arr = []
while(p.next) {
if(p.val == p.next.val) {
arr.push(p.val)
}
p = p.next
}
p = ret
while(p.next){
if(arr.includes(p.next.val)){
p.next = p.next.next
}else {
p = p.next
}
}
return ret.next
}; |
题目链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-list/description /**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteDuplicates = function(head) {
let last=head;
let node=head;
if(node)
{
node=node.next;
while(node)
{
if(node.val!=last.val)
{
last.next=node;
last=node;
}
node=node.next;
}
last.next=null;
}
return head;
}; |
/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteDuplicates = function(head) {
let p = head;
while(p && p.next) {
if (p.val === p.next.val) {
let next = p.next.next;
p.next = next;
} else {
p = p.next;
}
}
return head;
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: