leetcode上链表的题目还算不少的,暂时收录下面这些,可能有些被我分到其他部分去了。
code:
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int carry=0;
ListNode* dummy = new ListNode(0),*ite=dummy;
while(l1&&l2){
int num = l1->val+l2->val+carry;
carry = num/10;
num %=10;
ite->next = new ListNode(num);
ite = ite->next;
l1 = l1->next;
l2 = l2->next;
}
l1 = l2?l2:l1;
while(l1){
int num = l1->val+carry;
carry = num/10;
num %=10;
ite->next = new ListNode(num);
ite=ite->next;
l1 = l1->next;
}
if(carry){
ite->next = new ListNode(carry);
ite = ite->next;
}
ite->next = NULL;
ite = dummy->next;
delete dummy;
return ite;
}
};
这个题比较简单,写起来陷阱也不算多,一个改版是,如果链表的数字是倒过来的,怎么做?也就是说链表头是最高位的数字而不是最低位。
可以用递归做。
这个是很久之前写的代码,O(n)时间+O(n)空间。应该在代码风格和效率上还有可以优化的地方,暂时放在这里,以后有好的代码再补一个。
code:
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
ListNode* p1 = head;
if(head==NULL) return head;
vector<int> vec;
while(p1!=NULL){
vec.push_back(p1->val);
p1 = p1->next;
}
p1 = head;
bool sign = true;
int pos = 0;
while(p1!=NULL){
while(sign&&pos<vec.size()&&vec[pos]>=x)
pos++;
while(!sign&&pos<vec.size()&&vec[pos]<x)
pos++;
if(pos<vec.size()){
p1->val = vec[pos++];
p1 = p1->next;
}else{
if(!sign)
p1 = p1->next;
sign = false;
pos = 0;
}
}
return head;
}
};
关于这个题,还有一个很相似的版本,是要求O(n)时间+O(1)空间做到这个事情,不过给的不是链表,而是数组。我估摸着要是能做到,岂不是可以写出一个稳定的快排??不知道怎么搞。
Remove Duplicates from Sorted List
code:
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode *ite,*pre=head;
if(!head) return head;
ite = head->next;
while(ite){
if(ite->val==pre->val){
pre->next = ite->next;
delete ite;
}else
pre = ite;
ite = pre->next;
}
return head;
}
};
一开始没注意是sorted,写了个没有sorted的代码,顺便也附上:
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode* dummy = new ListNode(1),*ite,*pre=dummy;;
dummy->next = head;
ite = head;
set<int> s;
while(ite){
if(s.find(ite->val)!=s.end()){
pre->next = ite->next;
delete ite;
ite=pre->next;
}else{
s.insert(ite->val);
pre = ite;
ite = ite->next;
}
}
head = dummy->next;
delete dummy;
return head;
}
};
Remove Duplicates from Sorted List II
code:
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode* dummy = new ListNode(1);
dummy->next = head;
bool sign = false;
ListNode* pre = dummy,*ite=head;
while(ite){
if(sign||(ite->next&&ite->next->val==ite->val)){
if(!ite->next||ite->next->val!=ite->val)
sign =false;
else sign = true;
pre->next = ite->next;
delete ite;
}else{
pre=ite;
}
ite = pre->next;
}
head = dummy->next;
delete dummy;
return head;
}
};
Remove Nth Node From End of List
code:
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
ListNode** ite = &head, *runner = head,*tmp;
while(n--&&runner)
runner = runner->next;
if(n+1) return head;
while(runner)
{
runner = runner->next;
ite = &((*ite)->next);
}
tmp = *ite;
(*ite) = (*ite)->next;
delete tmp;
return head;
}
};
这个代码的思路就是让一个指针先走n步,那第一个指针到了尾之后,第二个指针就是倒数第n个,然后删掉。
另一个思路是递归,递归结束时记录当前是倒数第几个节点:
class Solution { public: int DelRecursive(ListNode* head,int n){ if(!head) return 0; int k = DelRecursive(head->next,n); if(n==k){ ListNode* tmp = head->next; head->next = head->next->next; delete tmp; } return k+1; } ListNode *removeNthFromEnd(ListNode *head, int n) { ListNode* dummy = new ListNode(0); dummy->next = head; DelRecursive(dummy,n); head = dummy->next; delete dummy; return head; } };
记住先断开再连接。class Solution { public: ListNode *reverseBetween(ListNode *head, int m, int n) { ListNode* dummy = new ListNode(1),*ite = dummy,*tail; dummy->next = head; for(int i=1;i<m;i++) ite=ite->next; head = ite; tail = ite->next; //先断开,再连接,顺序很重要,第m个不用管 for(int i=0;i<n-m;i++){ ite = tail->next; tail->next = ite->next; ite->next = head->next; head->next = ite; } head = dummy->next; delete dummy; return head; } };
为了体验一下指针的指针跟dummy head的区别,这个题分别用两种方法做了一遍:
2D Pointer code:
dummy head code:class Solution { public: ListNode *swapPairs(ListNode *head) { ListNode** ite = &head; while(*ite&&(*ite)->next) { ListNode* tmp = (*ite)->next; (*ite)->next = tmp->next; tmp->next = *ite; *ite = tmp; ite = &((*ite)->next->next); } return head; } };
class Solution { public: ListNode *swapPairs(ListNode *head) { ListNode* dummy = new ListNode(0),*ite=dummy; dummy->next = head; while(ite->next&&ite->next->next){ ListNode* tmp = ite->next->next;//保存要拿走的节点 ite->next->next = tmp->next; //删除该节点 tmp->next = ite->next; //把该节点接到ite后面 ite->next = tmp; ite = tmp->next; } ite = dummy->next; delete dummy; return ite; } };