Copy List with Random Pointer & Add Two Numbers & Valid Palindrome
2014-07-17 16:00
295 查看
(1) Copy List with Random Pointer
根据[1]:不用保存原始链表的映射关系,构建新节点时,指针做如下变化,即把新节点插入到相应的旧节点后面:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/20/3bfc63eb64d367a125a8d02928ae0663.jpg)
分两步:
1、构建新节点random指针:new1->random = old1->random->next, new2-random = NULL, new3-random = NULL, new4->random = old4->random->next
2、恢复原始链表以及构建新链表:例如old1->next = old1->next->next, new1->next = new1->next->next
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode *cur,*curnew,*ret;
cur=head;
if(!cur)
return cur;
while(cur)
{
curnew=new RandomListNode(cur->label);
curnew->next=cur->next;
cur->next=curnew;
cur=curnew->next;
}
cur=head;
curnew=head->next;
while(1)
{
if(cur->random)
curnew->random=cur->random->next;
if(!curnew->next)
break;
cur=cur->next->next;
curnew=cur->next;
}
cur=head;
curnew=head->next;
ret=curnew;
while(curnew->next)
{
cur->next=cur->next->next;
curnew->next=curnew->next->next;
cur=cur->next;
curnew=curnew->next;
}
cur->next=NULL;
return ret;
}
};
(2) Add Two Numbers
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int add1,add2,extra=0;
ListNode head(0),*cur,*pre;
pre=&head;
while(l1 || l2)
{
if(l1)
{
add1=l1->val;
l1=l1->next;
}
else
add1=0;
if(l2)
{
add2=l2->val;
l2=l2->next;
}
else
add2=0;
int sum=add1+add2+extra;
cur=new ListNode(sum%10);
pre->next=cur;
pre=cur;
extra=sum/10;
}
if(extra)
{
cur=new ListNode(1);
pre->next=cur;
}
return head.next;
}
};
(3) Valid Palindrome
class Solution {
bool isStr(char &c) {
if(c<='9' && c>='0')
return true;
if(c<='z' && c>='a')
return true;
if(c<='Z' && c>='A')
{
c+=32;
return true;
}
return false;
}
public:
bool isPalindrome(string s) {
if(s.size()==0)
return true;
int i=0,j=s.size()-1;
while(i<j)
{
while(!isStr(s[i]) && i<j)
i++;
while(!isStr(s[j]) && i<j)
j--;
if(s[i]!=s[j])
return false;
i++;
j--;
}
return true;
}
};
参考:
[1] http://www.2cto.com/kf/201310/253477.html
根据[1]:不用保存原始链表的映射关系,构建新节点时,指针做如下变化,即把新节点插入到相应的旧节点后面:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/20/3bfc63eb64d367a125a8d02928ae0663.jpg)
分两步:
1、构建新节点random指针:new1->random = old1->random->next, new2-random = NULL, new3-random = NULL, new4->random = old4->random->next
2、恢复原始链表以及构建新链表:例如old1->next = old1->next->next, new1->next = new1->next->next
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode *cur,*curnew,*ret;
cur=head;
if(!cur)
return cur;
while(cur)
{
curnew=new RandomListNode(cur->label);
curnew->next=cur->next;
cur->next=curnew;
cur=curnew->next;
}
cur=head;
curnew=head->next;
while(1)
{
if(cur->random)
curnew->random=cur->random->next;
if(!curnew->next)
break;
cur=cur->next->next;
curnew=cur->next;
}
cur=head;
curnew=head->next;
ret=curnew;
while(curnew->next)
{
cur->next=cur->next->next;
curnew->next=curnew->next->next;
cur=cur->next;
curnew=curnew->next;
}
cur->next=NULL;
return ret;
}
};
(2) Add Two Numbers
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int add1,add2,extra=0;
ListNode head(0),*cur,*pre;
pre=&head;
while(l1 || l2)
{
if(l1)
{
add1=l1->val;
l1=l1->next;
}
else
add1=0;
if(l2)
{
add2=l2->val;
l2=l2->next;
}
else
add2=0;
int sum=add1+add2+extra;
cur=new ListNode(sum%10);
pre->next=cur;
pre=cur;
extra=sum/10;
}
if(extra)
{
cur=new ListNode(1);
pre->next=cur;
}
return head.next;
}
};
(3) Valid Palindrome
class Solution {
bool isStr(char &c) {
if(c<='9' && c>='0')
return true;
if(c<='z' && c>='a')
return true;
if(c<='Z' && c>='A')
{
c+=32;
return true;
}
return false;
}
public:
bool isPalindrome(string s) {
if(s.size()==0)
return true;
int i=0,j=s.size()-1;
while(i<j)
{
while(!isStr(s[i]) && i<j)
i++;
while(!isStr(s[j]) && i<j)
j--;
if(s[i]!=s[j])
return false;
i++;
j--;
}
return true;
}
};
参考:
[1] http://www.2cto.com/kf/201310/253477.html
相关文章推荐
- leetcode -day8 Copy List with Random Pointer & Single Number I II
- 【小熊刷题】Copy List with Random Pointer <可再复习思路>
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
- Copy List with Random Pointer
- LeetCode:Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer [LeetCode]
- leetcode -- Copy List with Random Pointer
- LeetCode:Copy List with Random Pointer
- LeetCode Copy List with Random Pointer 分析解答
- Copy list with random pointer
- leetcode Copy List with Random Pointer
- [leetcode]Copy List with Random Pointer
- [LeetCode]Copy List with Random Pointer
- Leetcode: Copy List with Random Pointer
- Copy List with Random Pointer
- leetcode - Copy List with Random Pointer
- Copy List with Random Pointer
- CODE 135: Copy List with Random Pointer