这一部分需要重点复习。
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
.
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if(head == NULL || head->next == NULL) return head;
ListNode *p = head,*preq=head,*q=head->next;
while(q != NULL)
{
//system("pause");
//printf("q=%d\n",q->val);
//printList(head,"head");
if(q->val < x)
{
if(p->val>=x)
{
preq->next = q->next;
q->next = head;
head = q;
p = head;
q = preq;
}
else
{
if(preq != p)
{
preq->next = q->next;
q->next = p->next;
p->next = q;
p = q;
//printf("p=%d\n",p->val);
q = preq;
}
else
p=p->next;
}
}
preq = q;
q = q->next;
}
return head;
}
};
Given a linked list, reverse the nodes of a linked list k at a time and return its modified 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
class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
if(head == NULL || head->next == NULL || k == 1) return head;
int i = 0;
ListNode *tmphead = head, *tmptail = head, *tmpSaveHead = NULL,*tmpSaveTail = NULL , *p;
while(tmptail != NULL)
{
while(++i<k && tmptail != NULL)
tmptail = tmptail->next;
if(tmptail == NULL) return head;
tmpSaveTail = tmptail->next;
//printList(tmphead,"tmphead");
p = tmphead->next;
tmptail = tmphead;
while(i-- > 1)
{
tmptail->next = p->next;
p->next = tmphead;
tmphead = p;
p = tmptail->next;
}
if(tmpSaveHead) tmpSaveHead->next = tmphead;
else head = tmphead;
tmpSaveHead = tmptail;
//printList(tmphead,"1tmphead");
//system("pause");
//printList(head,"head");
//system("pause");
tmphead = tmpSaveTail;
tmptail = tmpSaveTail;
i = 0;
}
return head;
}
};
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.
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
if(head == NULL || head->next == NULL || m >= n) return head;
int i = 1;
ListNode *p = head;
while(++i<m && p != NULL) p = p->next;
if(p == NULL) return head;
ListNode *saveHead = p;
if(m > 1)
p = p->next;
ListNode *tmphead = p, *tmptail = p;
i = m;
p = p->next;
while(++i<=n && p != NULL)
{
tmptail->next = p->next;
p->next = tmphead;
tmphead = p;
p = tmptail->next;//////////////////////////
}
if(m>1)
saveHead->next = tmphead;
else
head = tmphead;
return head;
}
};
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(head == NULL) return NULL;
ListNode *p = head;
ListNode *q = p->next;
while(q != NULL)
{
if(p->val == q->val)//把q移除
{
p->next = q->next;
q = p->next;
}
else
{
q = q->next;
p = p->next;
}
}
return head;
}
};
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(head == NULL || head->next == NULL) return head;
ListNode *p = head,*q = head->next;
int tmpval = 0;
int num = 0;
while(q != NULL)
{
while(q != NULL && p->val == q->val)
{
p = q;
q=q->next;
num++;
}
if(q == NULL) return NULL;
if(num > 0)
{
head = q;
p = head;
q = head->next;
num = 0;
}
else
break;
}
if(q == NULL) return head;
tmpval = q->val;
//ListNode *preq = q;
q = q->next;
num = 0;
while(q != NULL)
{
if(q->val == tmpval)
{
num++;
}
else
{
if(num >0)
p->next = q;
else
p = p->next;
tmpval = q->val;
num = 0;
}
q = q->next;
}
if(num > 0)
p->next = NULL;
return head;
}
};