2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Solution:考察链表知识,直接按十进制运算和进位即可。
Code:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* ans = l1;
ListNode* end = l1;
int addnum = 0;
while(l1!=NULL && l2!=NULL){
l1->val = l1->val + l2->val + addnum;;
addnum = l1->val / 10;
l1->val = l1->val % 10;
end = l1;
l1 = l1->next;
l2 = l2->next;
}
if(l2!=NULL){
end->next = l2;
end = l2;
l1 = l2;
}
while(l1!=NULL){
l1->val = l1->val + addnum;
addnum = l1->val / 10;
l1->val = l1->val % 10;
end = l1;
l1 = l1->next;
}
if(addnum!=0){
end->next = new ListNode(addnum);
}
return ans;
}
};
92. Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL
, m = 2 and n = 4,
return 1->4->3->2->5->NULL
.
Note:
Given m, n satisfy the following condition:
1 大于等于 m 大于等于 n 大于等于 length of list.
Solution: 直接做就好,此题难点在于非常繁琐,有很多边界检查。对于m-n之间的节点,将每一个节点指向它的前一个。
Code:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(m==n) return head;
ListNode* pre = NULL;
ListNode* cur = head;
ListNode* next = NULL;
ListNode* start = NULL;
ListNode* end = NULL;
int count = 0;
while(cur!=NULL){
count++;
cout<<count<<endl;
if(count==m){
start = pre;
end = cur;
}else if(count==n){
if(start!=NULL) start->next = cur;
else head = cur;
end->next = cur->next;
}
if(count>=m && count<=n){
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}else{
pre = cur;
cur = cur->next;
}
}
return head;
}
};
Solution-plus: 简化代码。加入需旋转的新节点时,将其插入到m点之前即可。
Code:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode dummy(-1); //避免m点就是第一点的情况
dummy.next = head;
int count = 0;
ListNode* prev_m = NULL;//记录m点前一点
ListNode* prev = &dummy;
while(head!=NULL){
count++;
if(count==m){
prev_m = prev;
}
if(count>m && count<=n){
//将当前节点插入到m点之前
prev->next = head->next;
head->next = prev_m->next;
prev_m->next = head;
head = prev;
}
prev = head;
head = head->next;
}
return dummy.next;
}
};
86. Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given 1->4->3->2->5->2
and x = 3,
return 1->2->2->4->3->5
.
Code:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode leftdummy = ListNode(0);
ListNode rightdummy = ListNode(0);
ListNode* leftcur = &leftdummy;
ListNode* rightcur = &rightdummy;
while(head!=NULL){
if(head->val<x){
leftcur->next = head;
leftcur = head;
}else{
rightcur->next = head;
rightcur = head;
}
head = head->next;
}
rightcur->next = NULL;
leftcur->next = rightdummy.next;
return leftdummy.next;
}
};