Chain rearrangement Leetcode 141

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Mr_zhuo_/article/details/88548844

The first idea :( Time: 144ms Memory: 12MB )

           1. pointer to find the midpoint

           2. split into two lists, split into two longitudinal chains line1, line2. Retrograde line2.

           3. traverse two lists, behind the front of the stuffed "crevice."

Node is an even number: size (line1) = size (line2)

When a node is odd: size (line1) = size (line2) +1;

Eliminating the need for each to find the end of the chain of time. When the list is very long effect. However, the memory has not improved significantly.

The second idea: deque

Pros: Simple

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

//第一种思路
class Solution {
public:
	void reorderList(ListNode* head) {
		if (head == NULL || head->next == NULL) return;

		ListNode* p = head, *q;
		int count = 0;
		while (p != NULL)
		{
			p = p->next;
			count++;
		}
		p = head;
		//找中间位置
		for (int i = 1; i < (count +1) / 2; i++)//len(line1)>=len(line2)
			p = p->next;
		//拆成两条链,添加头结点
  		ListNode* line1 = new ListNode(-1);
		line1->next = head;
		ListNode* line2 = new ListNode(-1);
		line2->next = p->next;
		p->next = NULL;
		//逆置第二条链
		reverse(line2);

		p = line1->next;
		q = line2->next;
        
		while (p&&q)
		{
			ListNode* tmp = q;
			q = q->next;
			tmp->next = p->next;
			p->next = tmp;
			p = p->next->next;
		}
		head = line1->next;
		return;
	}

	void reverse(ListNode *head)
	{
		ListNode*p = head->next;
		head->next = NULL;
		while (p)
		{
			ListNode* q = p;//这里千万要记录p,否则p->next变成了NULL
			p = p->next;
			q->next = head->next;
			head->next =q;
		}
	}

};


//第二种思路
class Solution {
    public void reorderList(ListNode head) {
        LinkedList<ListNode> queue = new LinkedList<>();
        ListNode cur = head;
        while (cur != null) {
            queue.addLast(cur);
            cur = cur.next;
        }
        while (!queue.isEmpty()) {
            if (cur == null) {
                cur = queue.pollFirst();
            } else {
                cur.next = queue.pollFirst();
                cur = cur.next;
            }
            cur.next = queue.pollLast();
            cur = cur.next;
        }
        if (cur != null) {
            cur.next = null;
        }
    }
}

 

Guess you like

Origin blog.csdn.net/Mr_zhuo_/article/details/88548844