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.
提示:carry保存进位
答案:
/**
* 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 preHead(0), *p = &preHead;
int carry = 0;
while(l1 || l2 || carry){
if(l1) carry += l1 -> val, l1 = l1 -> next;//注意这里是
if(l2) carry += l2 -> val, l2 = l2 -> next;
p -> next = new ListNode(carry % 10);
carry /= 10;
p = p -> next;
}
return preHead.next;
}
};
19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head.
Example:
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Follow up:
Could you do this in one pass?
提示: fast,slow
答案:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode **slow = &head, *fast = head;
for(int i = 1; i < n; i++){//注意i=1,为了让slow停在删除结点
fast = fast -> next;
}
while(fast -> next != NULL){
fast = fast -> next;
slow = &((*slow) -> next);
}
*slow = (*slow) -> next;
return head;
}
};
21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4
提示:创建一个结点等于 两个链表较小的,然后加入新链表
答案:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode dummy(0);
ListNode *tail = &dummy;
while(l1 && l2){
ListNode *&node = l1 -> val < l2 -> val ? l1 : l2;
tail = tail -> next = node;
node = node -> next;
}
tail -> next = l1? l1 : l2;
return dummy.next;
}
};
24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
Example:
Given1->2->3->4
, you should return the list as2->1->4->3
.
Note:
- Your algorithm should use only constant extra space.
- You may not modify the values in the list's nodes, only nodes itself may be changed.
提示:next=第3结点,交换1、2结点,preNode保存1结点并且其next设为NULL。循环结束处理个数为奇数的情况。
答案:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == NULL || head -> next == NULL) return head;
ListNode dummy(0);
ListNode *preNode = &dummy;
ListNode *node = head;
while(node != NULL && node -> next != NULL){ //1->2->3->4
ListNode *next = node -> next -> next; //next = 3
node -> next -> next = node; //2->1
preNode -> next = node -> next; //first loop: dummy->2 . second loop: 1->4.
preNode = node; // preNode = 1. preNode = 3.
preNode -> next = NULL;
node = next;
}
if(node) preNode -> next = node; //when the size of the Linked List is odd
return dummy.next;
}
};
61. Rotate List
Given a linked list, rotate the list to the right by k places, where k is non-negative.
Example 1:
Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3->4->NULL rotate 2 steps to the right: 4->5->1->2->3->NULL
提示:tail node是 (len - k) - th node
答案:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head || ! head->next || k == 0) return head;
ListNode *tail = head;
int len = 1; // number of nodes
while(tail -> next && ++len) tail = tail -> next;
tail -> next = head;// circle the link
k = len - k % len;
// the tail node is the (len-k)-th node (1st node is head)
while(k--) tail = tail -> next;
head = tail -> next;
tail -> next = NULL;
return head;
}
};
82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5 Output: 1->2->5
提示:dummy后面两个结点如果相等,那么记录该值,删除两个结点。
答案:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head){
if(head == NULL || head->next == NULL) return head;
ListNode dummy(0);
dummy.next = head;
head = &dummy;
while(head->next && head->next->next){
if(head->next->val == head->next->next->val){
int val = head->next->val;
while(head->next && head->next->val == val){
head->next = head->next->next;
}
}else{
head = head->next;
}
}
return dummy.next;
}
};
83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2 Output: 1->2
提示:递归 三行
答案:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == NULL || head -> next == NULL) return head;
head -> next = deleteDuplicates(head -> next);
return head -> val == head -> next -> val ? head -> next : head;
}
};
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.
Example:
Input: head = 1->4->3->2->5->2, x = 3 Output: 1->2->2->4->3->5
提示:node1是小于x结点的头,node2是大于等于x结点的头,使用指针p1、p2,最终返回node1.next
答案:
/**
* 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 node1(0), node2(0);
ListNode *p1 = &node1, *p2 = &node2;
while (head) {
if (head->val < x)
p1 = p1->next = head;
else
p2 = p2->next = head;
head = head->next;
}
p2->next = NULL;
p1->next = node2.next;
return node1.next;
}
};