跳至主要內容

86. 分隔链表

T4mako算法链表双指针小于 1 分钟

86. 分隔链表

题目描述open in new window

中等

解法思路:创建新链表,遍历链表

class Solution {
    public ListNode partition(ListNode head, int x) {
		if(head == null || head.next == null) return head;
		ListNode l = new ListNode(0);
		ListNode le = l;
		ListNode r = new ListNode(0);
		ListNode re = r;
		ListNode node = head;
		while(node != null) {
			if(node.val < x) {
				le.next = new ListNode(node.val);
				le = le.next;
			}else {
				re.next = new ListNode(node.val);
				re = re.next;
			}
			node = node.next;
		}
		le.next = r.next;
		return l.next;
    }
}
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5