leetcode上链表的题目还算不少的,暂时收录下面这些,可能有些被我分到其他部分去了。
Add Two Numbers
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;
}
};
这个题比较简单,写起来陷阱也不算多,一个改版是,如果链表的数字是倒过来的,怎么做?也就是说链表头是最高位的数字而不是最低位。
Partition List
这个是很久之前写的代码,O(n)时间+O(n)空间。应该在代码风格和效率上还有可以优化的地方,暂时放在这里,以后有好的代码再补一个。
code:
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
ListNode* p1 = head;
if(head==NULL) return head;
vector 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=x)
pos++;
while(!sign&&posval = vec[pos++];
p1 = p1->next;
}else{
if(!sign)
p1 = p1->next;
sign = false;
pos = 0;
}
}
return head;
}
};
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;
}
};
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode* dummy = new ListNode(1),*ite,*pre=dummy;;
dummy->next = head;
ite = head;
set 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;
}
};
Reverse Linked List II
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;inext;
head = ite;
tail = ite->next;
//先断开,再连接,顺序很重要,第m个不用管
for(int i=0;inext;
tail->next = ite->next;
ite->next = head->next;
head->next = ite;
}
head = dummy->next;
delete dummy;
return head;
}
};
记住先断开再连接。
Swap Nodes in Pairs
为了体验一下指针的指针跟dummy head的区别,这个题分别用两种方法做了一遍:
2D Pointer 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;
}
};
dummy head code:
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;
}
};