leetcode之Partition List,Reverse Nodes in k-Group

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;
    }
};

leetcode之Partition List,Reverse Nodes in k-Group

时间: 2024-10-28 10:46:41

leetcode之Partition List,Reverse Nodes in k-Group的相关文章

LeetCode(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. 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

LeetCode之“链表”: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 node

leetcode第24题--Reverse Nodes in k-Group

problem: 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,

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

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

[LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表

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

leetCode 25.Reverse Nodes in k-Group (以k个节点为一组反转链表) 解题思路和方法

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 valu

Leetcode 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 nod

[LeetCode 题解]: Reverse Nodes in K-Groups

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 nod

LeetCode:Reverse Nodes in k-Group

1.题目名称 Reverse Nodes in k-Group(分组翻转链表) 2.题目地址 https://leetcode.com/problems/reverse-nodes-in-k-group 3.题目内容 英文: 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 multipl