关闭

【Leetcode】Reorder List

267人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/reorder-list/

题目:

Given a singly linked list LL0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-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}.

思路:

划分后半段链表,找到它的表头。然后将它反转,再和前半段链表交叉合并。其中划分链表、反转部分代码在Palindrome Linked List 中已有实现,

只需添加交叉合并操作就可以了。

算法:

	public void reorderList(ListNode head) {
		if (head == null || head.next == null) {
			return;
		}
		int i = getListLength(head) / 2;
		ListNode p = head, l2 = null;
		while (i-- > 0)
			p = p.next;
		l2 = p.next; //此时l2指向后半段链表表头
		p.next = null;
		
		l2 = reverseList(l2);// 反转后半段链表

		p = head;
		while (l2 != null) {// 将两链表交叉合并,在p~t之间插入l2结点,其中t2指向l2后面结点
			ListNode t = p.next;
			ListNode t2 = l2.next;
			p.next = l2;
			l2.next = t;
			l2 = t2;
			p = t;
		}
	}

	public int getListLength(ListNode head) {
		int length = 0;
		ListNode p = head;
		while (p != null) {
			length++;
			p = p.next;
		}
		return length;
	}

	public ListNode reverseList(ListNode head) {
		ListNode p = head, q, t;
		if (p == null)
			return head;
		q = p.next;
		while (q != null) {
			t = q.next;// 保存当前要处理结点后面的一个结点
			q.next = p;
			p = q; // p是新链表头结点
			q = t;
		}
		head.next = null;// 原头结点变成尾节点
		head = p;
		return head;
	}


0
0
查看评论

LeetCode-143. Reorder List (JAVA)重排链表

LeetCode-143. Reorder List (JAVA)重排链表
  • mine_song
  • mine_song
  • 2017-05-12 20:34
  • 638

【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 n...
  • ljiabin
  • ljiabin
  • 2014-08-29 22:52
  • 4274

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 al...
  • u012560212
  • u012560212
  • 2017-05-13 21:52
  • 245

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' ...
  • feliciafay
  • feliciafay
  • 2014-02-01 07:58
  • 1649

[leetcode] 143. Reorder List 解题报告

题目链接:https://leetcode.com/problems/reorder-list/ Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→...
  • qq508618087
  • qq508618087
  • 2016-01-08 08:18
  • 1186

Leetcode在线编程reorder-list

Leetcode在线编程reorder-list 快慢指针找链表中点,链表反转
  • zero_witty
  • zero_witty
  • 2016-12-03 00:31
  • 151

[leetcode][list] 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 al...
  • Clairezz_
  • Clairezz_
  • 2015-05-22 18:00
  • 153

[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 n...
  • bhwolf1987
  • bhwolf1987
  • 2013-11-04 22:26
  • 7504

Reorder List[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 ...
  • everthing99
  • everthing99
  • 2014-04-13 14:20
  • 542

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 n...
  • u013195320
  • u013195320
  • 2014-06-23 23:16
  • 727
    个人资料
    • 访问:569668次
    • 积分:8680
    • 等级:
    • 排名:第2679名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论