leetCode(6):Reorder list

Given a singly linked list L: L0→L1→…→Ln-1→Ln,

reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes‘ values.

For example,

Given {1,2,3,4}, reorder it to {1,4,2,3}.

常规版本:遍历链表的方式,事实证明,遍历永远不是最好的!

void reorderList(ListNode* head)
{//递归、循环效果应该差不多
	ListNode* p=head;
	if(head==NULL || head->next==NULL)
		return;
	ListNode* currentNode=p;
	ListNode* nextNode=p->next;
	ListNode* newNextNode=nextNode;

	while(nextNode && nextNode->next)
	{
		ListNode* preNewNextNode=currentNode;
		ListNode* prePreNewNextNode=currentNode;
		while(newNextNode->next)
		{
			preNewNextNode=preNewNextNode->next;
			newNextNode=newNextNode->next;
		}
		prePreNewNextNode->next=NULL;
		preNewNextNode->next=NULL;

		currentNode->next=newNextNode;
		newNextNode->next=nextNode;
		currentNode=nextNode;
		nextNode=currentNode->next;
		newNextNode=nextNode;
	}
}

反转子链表版本:用两个指针,一快一慢遍历整个链表,直到较快的链表到达末尾,慢指针刚好到达中点,然后断开链表。把后段链表反转,再逐个插入。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head)
    {
    	if(!head)
    		return head;
    	ListNode* p=head;
    	ListNode* q=p->next;
    	if(!q)
    		return head;
    	ListNode* k=q->next;
    	p->next=NULL;
    	while(k)
    	{
    		q->next=p;
    		p=q;
    		q=k;
    		k=k->next;
    	}
    	q->next=p;
    	return q;//返回头结点
    }
    void reorderList(ListNode* head) {
    	if(head==NULL || head->next==NULL)
    		return;
    	ListNode* p=head;
    	ListNode* fast=p;
    	ListNode* slow=p;
    	while(fast && fast->next)
    	{
    		fast=fast->next->next;
    		slow=slow->next;
    	}
    	ListNode* subHead=slow->next;
    	slow->next=NULL;
    	subHead=reverseList(subHead);
    	ListNode* p2=subHead;
    	while(p2!=NULL)
    	{
    		ListNode* tmp1=p->next;
    		ListNode* tmp2=p2->next;
    		p->next=p2;
    		p2->next=tmp1;
    		p2=tmp2;
    		p=tmp1;
    	}
    }
};

时间: 2024-08-06 15:41:26

leetCode(6):Reorder list的相关文章

【Leetcode】Reorder List JAVA

一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 二.分析 1.暴力解法 这种解法所需的时间的时间复杂度比较高,为O(n2) 代码如下

[LeetCode] String Reorder Distance Apart

Question: Given a string of lowercase characters, reorder them such that the same characters are at least distance d from each other. Input: { a, b, b }, distance = 2 Output: { b, a, b } http://leetcode.com/2010/05/here-is-another-google-phone-interv

[LeetCode] 937. Reorder Data in Log Files 日志文件的重新排序

You have an array of?`logs`.? Each log is a space delimited string of words. For each log, the first word in each log is an alphanumeric?identifier.? Then, either: Each word after the identifier will consist only of lowercase letters, or; Each word a

leetcode 【 Reorder List 】python 实现

题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 代码: oj 测试通过 248 ms 1 # Definition for singly-

[Leetcode][JAVA] Reorder List

Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 比较容易思考且实现的一个思路是, 将链表从中心拆成两半,后一半全部反向连接,然后前一半和后一半一个

Java for LeetCode 143 Reorder List

Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. 解题思路一: 每次将Ln换到前面,得到L0→Ln→L1→L2→L3→,然后对L1使用相同操作,

【LeetCode】Reorder List

Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * clas

【leetcode】Reorder List (middle)

Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 思路: 先把链表分成两节,后半部分翻转,然后前后交叉连接. 大神的代码比我的简洁,注意分两节时用快

【LeetCode】Reorder List 解题报告

Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * clas

[LeetCode]97. Reorder List链表重排序

Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. Subscribe to see which companies asked this que