我试图解决这个问题并且我的代码中存在一个错误,但我无法弄清楚原因:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/
给定一个已排序的链接列表,删除所有具有重复数字的节点,只留下原始列表中的不同数字。
例如, 给定1-> 2-> 3-> 3-> 4-> 4-> 5,返回1-> 2-> 5。 给定1-> 1-> 1-> 2-> 3,返回2-> 3.
错误是
Line 44: TypeError: Cannot read property 'val' of null
以下是Javascript中的代码
function ListNode(val) {
this.val = val;
this.next = null;
}
var reverseBetween = function(head, m, n) {
if(m>=n) return head;
var currentNode = head;
var previousNode = null;
var temp = null, tempStart = null;
var countm = 0, countn=0;
if(!currentNode.next) return head;
var startNode = null;
console.log([head.val, head.next.val, head.next.next.val, head.next.next.next.val, head.next.next.next.next.val]);
while(countm<m){
//advance pointer and do nothing
startNode = previousNode;
previousNode = currentNode;
currentNode = currentNode.next;
countm++;
console.log(previousNode.val + ', ' + currentNode.val)
}
console.log('before: startNode: ' + startNode.val + ', prev: ' + previousNode.val + ', curr: ' +currentNode.val)
while(countn < (n-m)){
temp = currentNode.next;
currentNode.next = previousNode;
startNode.next = currentNode;
previousNode.next = temp;
countn++;
console.log('startNode: ' + startNode.val + ', prev: ' + previousNode.val + ', curr: ' +currentNode.val)
console.log([head.val, head.next.val, head.next.next.val, head.next.next.next.val, head.next.next.next.next.val]);
//advance
previousNode = currentNode;
currentNode = currentNode.next;
console.log('startNode: ' + startNode.val + ', prev: ' + previousNode.val + ', curr: ' +currentNode.val)
}
return head;
};
var a = new ListNode(1);
var b = new ListNode(2);
var c = new ListNode(3);
var d = new ListNode(4);
var e = new ListNode(5);
a.next = b;
b.next = c;
c.next = d;
d.next = e;
reverseBetween(a, 2, 4)
答案 0 :(得分:1)
function ListNode(val) {
this.val = val;
this.next = null;
}
var reverseBetween = function(head, m, n) {
if(m>=n) return head;
var currentNode = head;
var previousNode = null;
var temp = null, tempStart = null;
var countm = 0, countn=0;
if(!currentNode.next) return head;
var startNode = null;
console.log([head.val, head.next.val, head.next.next.val, head.next.next.next.val, head.next.next.next.next.val]);
while(countm<m){
//advance pointer and do nothing
startNode = previousNode;
previousNode = currentNode;
currentNode = currentNode.next;
countm++;
console.log(previousNode.val + ', ' + currentNode.val)
}
console.log('before: startNode: ' + startNode.val + ', prev: ' + previousNode.val + ', curr: ' +currentNode.val)
while(countn < (n-m)){
temp = currentNode.next;
temp2 = previousNode;
currentNode.next = previousNode;
startNode.next = currentNode;
previousNode.next = temp;
countn++;
console.log('startNode: ' + startNode.val + ', prev: ' + previousNode.val + ', curr: ' +currentNode.val)
console.log([head.val, head.next.val, head.next.next.val, head.next.next.next.val, head.next.next.next.next.val]);
//advance
previousNode = temp2;
currentNode = currentNode.next;
console.log('startNode: ' + startNode.val + ', prev: ' + previousNode.val + ', curr: ' +currentNode.val)
}
return head;
};
var a = new ListNode(1);
var b = new ListNode(2);
var c = new ListNode(3);
var d = new ListNode(4);
var e = new ListNode(5);
a.next = b;
b.next = c;
c.next = d;
d.next = e;
reverseBetween(a, 2, 4)