24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4
, you should return the list as 2->1->4->3
.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
Solution: 直接swap即可。
Code:
/**
* 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) return head;
ListNode dummy(0);
dummy.next = head;
ListNode* prev = &dummy;
ListNode* next = NULL;
while(head->next!=NULL){
next = head->next; //prev,head,next
head->next = next->next;
next->next = head;
prev->next = next; //prev,next,head
prev = head;//next,head(prev)
head = head->next; //next,prev,head
if(head==NULL) break;
}
return dummy.next;
}
};
25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
Code:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if(head==NULL || k<=1) return head;
ListNode dummy(0);
dummy.next = head;
//每次翻转一节(k个),最后部分如果不足k个在翻转1次后再翻转1次复原
ListNode* begin = &dummy;
ListNode* end = &dummy;
int i;
while(head!=NULL){
i = 2;
begin = end;
end = end->next;
head = head->next;
while(i<=k && head!=NULL){
//将该点连接到begin后面: begin,...,end
end->next = head->next;
head->next = begin->next;
begin->next = head; //begin,head,...,end
head = end->next;
i++;
}
}
//当最后一节不足k个时,多翻转1次复原顺序
end = begin->next;
if(end!=NULL) head = end->next;
while(i<=k && head!=NULL){
//将该点连接到begin后面: begin,...,end
end->next = head->next;
head->next = begin->next;
begin->next = head; //begin,head,...,end
head = end->next;
}
return dummy.next;
}
};
138. Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
Solution(1): 首先建好一个新的链表,然后更新random指针,时间复杂度O(n^2)。Code:
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode* oldhead = head;
RandomListNode dummy(0);
//深拷贝一个新的单链表(random直接拷贝)
int i = 1;
RandomListNode* prev = &dummy;
while(head!=NULL){
RandomListNode* newRln = new RandomListNode(head->label);
newRln->random = head->random;
prev->next = newRln;
prev = newRln;
head = head->next;
}
head = dummy.next;
//设置真正的random
while(head!=NULL){
head->random = getRandom(oldhead, dummy.next, head->random);
head = head->next;
}
return dummy.next;
}
private:
RandomListNode *getRandom(RandomListNode *oldhead, RandomListNode *begin, RandomListNode* random){
//返回新链表中对应的结点处的random指针
if(random==NULL) return NULL;
else{
while(oldhead!=NULL && oldhead!=random){
oldhead = oldhead->next;
begin = begin->next;
}
return begin;
}
}
};
Solution(2): 将旧链表每个结点之后增加一个(1->2->3 to 1->1->2->2->3->3),然后将其拆分成两个链表。
Code:
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if(head==NULL) return head;
RandomListNode* oldhead = head;
//链表增倍
while(head!=NULL){
RandomListNode* newRln = new RandomListNode(head->label);
newRln->next = head->next;
newRln->random = head->random;
head->next = newRln;
head = newRln->next;
}
//遍历新增结点,设置random指针
head = oldhead;
while(head!=NULL){
head = head->next;
if(head->random!=NULL)
head->random = head->random->next;
head = head->next;
}
//拆分链表
RandomListNode dummy(0);
head = oldhead;
dummy.next = head->next;
RandomListNode* cur = &dummy;
while(head!=NULL){
cur->next = head->next;//串联拷贝链表结点
cur = cur->next;
head->next = head->next->next;//串联原链表结点
head = head->next;
}
return dummy.next;
}
};