您的位置:首页 > Web前端 > Node.js

leetcode之Partition List,Reverse Nodes in k-Group

2014-08-21 18:46 387 查看
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
.

题目比较简单,这里添加一个辅助节点,这样就不用分情况对第一个节点进行讨论了
struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
    	ListNode* beforeHead = new ListNode(INT_MIN);//辅助节点,防止对头节点的操作
    	beforeHead -> next = head;
    	ListNode* p1 = beforeHead,*p2 = beforeHead,*p3 = head;
    	while(p3)
    	{
    		if(p3 -> val < x && p2 -> val >= x)//p2 -> val >= x防止节点原地插入
    		{
    			p2 -> next = p3 -> next;
    			p3 -> next = p1 -> next;
    			p1 -> next = p3;
    			p1 = p3;
    			p3 = p2 -> next;
    			continue;
    		}
    		if(p3 -> val < x)p1 = p1 -> next;
    		p2 = p3;
    		p3 = p3 -> next;
    	}
    	head = beforeHead -> next;
    	delete beforeHead;
    	return head;
    }
};




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.

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


题目比较简单,这里添加一个辅助节点,这样就不用分情况对第一个节点进行讨论了,链表的题目比较简单,但要是能在短时间内做到没有错误,很困难

struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *reverseKGroup(ListNode *head, int k) {
    	if(k <= 1 || !head)return head;
    	ListNode* beforeHead = new ListNode(0);
    	beforeHead -> next = head;
    	ListNode* p1 = beforeHead,*p2 = head,*p3 = head;
    	while(p3)
    	{
    		int i;
    		for(i = 0;i < k;++i)
    		{
    			if(!p3)break;
    			p3 = p3 -> next;//p3代表下一个反转的开始节点
    		}
    		if(i < k)break;//表示最后一步没有k个节点,直接返回
    		ListNode* p = p2 -> next;
    		while(p != p3)//把p2 到p3之间进行反转
    		{
    			p2 -> next = p -> next;
    			p -> next = p1 -> next;
    			p1 -> next = p;
    			p = p2 -> next;
    		}
    		p1 = p2;
    		p2 = p3;
    	}
    	head = beforeHead -> next;
    	delete beforeHead;
    	return head;
    }
};


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.

For example,

Given
1->2->3->3->4->4->5
, return
1->2->5
.

Given
1->1->1->2->3
, return
2->3
.

这三个题目都很简单,相同的地方都是使用了一个辅助节点

struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
    	if(!head || head -> next == NULL)return head;
    	ListNode* beforeHead = new ListNode(0);
    	beforeHead -> next = head;
    	ListNode* p1 = beforeHead,*p2 = head,*p3 = head -> next;
    	while(p3)
    	{
    		if(p2->val == p3->val)
    		{
    			while(p2->val == p3->val)//p3指向下一个不相同的节点
    			{	
    				p3 = p3 -> next;
    				if(!p3)break;
    			}
    			while(p1->next != p3)//删除p1到p3之间的节点
    			{
    				p1 -> next = p2 -> next;
    				delete p2;
    				p2 = p1 -> next;
    			}
    			if(p3) p3 = p3 -> next;
    			continue;
    		}
    		p1 = p2;
    		p2 = p3;
    		p3 = p3 -> next;
    	}
    	head = beforeHead -> next;
    	delete beforeHead;
    	return head;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: