重排
重排的话,看能不能转换成两个规定的链表,再将两个链表转为一个链表。
86. 分隔链表(中等)
思路:定义两个链表,分别存储小于和大于等于x的节点,最后直接拼接。
public ListNode partition(ListNode head, int x) {
ListNode re =new ListNode(0);
ListNode small = re;
ListNode end = new ListNode(0);
ListNode big = end;
while(head!=null){
if(head.val<x){
small.next = head;
small = small.next;
}else{
big.next = head;
big = big.next;
}
head = head.next;
}
big.next = null;
small.next = end.next;
return re.next;
}
328. 奇偶链表(中等)
思路:定义两个链表,分别存储奇数和偶数的节点,最后直接拼接。
public ListNode oddEvenList(ListNode head) {
ListNode oddHead = new ListNode(-1);
ListNode evenHead = new ListNode(-1);
ListNode oddCur = oddHead, evenCur = evenHead;
int i = 1;
while (head != null){
if (i % 2 == 1){
oddCur.next = head;
oddCur = oddCur.next;
}else{
evenCur.next = head;
evenCur = evenCur.next;
}
head = head.next;
i++;
}
evenCur.next = null;
oddCur.next = evenHead.next;
return oddHead.next;
}
143. 重排链表(中等)
思路:找出中间节点,分成两个链表,中间节点后的链表反转,两个链表再合并。
中间节点:
public ListNode middleNode(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(fast != null) {
if(fast.next != null && fast.next.next != null) {
fast = fast.next.next;
}
else
break;
slow = slow.next;
}
return slow;
}
slow的下一个节点就是后面链表的头节点,然后slow.next = null。
第二种写法:
ListNode slow = head, fast = head;
while (fast != tail) {
slow = slow.next;
fast = fast.next;
if (fast != tail) {
fast = fast.next;
}
}
ListNode mid = slow;
ListNode list1 = sortList(head, mid);
ListNode list2 = sortList(mid, tail);
ListNode sorted = merge(list1, list2);
这种适合归并排序,拆分成head->slow和slow->fast。slow和fast分别是两个链表的tail。
排序
148. 排序链表(中等)
归并排序:
public ListNode sortList(ListNode head) {
return sortList(head, null);
}
public ListNode sortList(ListNode head, ListNode tail) {
if (head == null) {
return head;
}
if (head.next == tail) {
head.next = null;
return head;
}
ListNode slow = head, fast = head;
while (fast != tail) {
slow = slow.next;
fast = fast.next;
if (fast != tail) {
fast = fast.next;
}
}
ListNode mid = slow;
ListNode list1 = sortList(head, mid);
ListNode list2 = sortList(mid, tail);
ListNode sorted = merge(list1, list2);
return sorted;
}
public ListNode merge(ListNode head1, ListNode head2) {
ListNode dummyHead = new ListNode(0);
ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
while (temp1 != null && temp2 != null) {
if (temp1.val <= temp2.val) {
temp.next = temp1;
temp1 = temp1.next;
} else {
temp.next = temp2;
temp2 = temp2.next;
}
temp = temp.next;
}
if (temp1 != null) {
temp.next = temp1;
} else if (temp2 != null) {
temp.next = temp2;
}
return dummyHead.next;
}
147. 对链表进行插入排序(中等)
思路:要不断比较cur.next与front.next,两者都要用next,因为要删除cur.next和在front后插入节点,方便操作。
优化点:如果当前节点比前一个节点大,就直接下一次循环,因为已经有序。
if(cur.val<cur.next.val){
cur = cur.next;
continue;
}
完整程序:
public ListNode insertionSortList(ListNode head) {
if(head == null)
return null;
if(head.next == null)
return head;
ListNode dummyHead = new ListNode(-1,head);
ListNode cur = dummyHead.next;
while(cur.next != null) {
ListNode front = dummyHead;
if(cur.val < cur.next.val){
cur = cur.next;
continue;
}
while(cur.next.val > front.next.val) {
front = front.next;
}
ListNode node = cur.next;
cur.next = cur.next.next;
node.next = front.next;
front.next = node;
}
return dummyHead.next;
}