1.编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
if(pHead == null) {
return null;
}
if(pHead.next == null) {
return pHead;
}
ListNode smallHead = new ListNode(1);
ListNode smallTail = smallHead;
ListNode bigHead = new ListNode(1);
ListNode bigTail = bigHead;
ListNode cur = pHead;
while(cur != null) {
if(cur.val < x) {
smallTail.next = new ListNode(cur.val);
smallTail = smallTail.next;
cur = cur.next;
}else {
bigTail.next = new ListNode(cur.val);
bigTail = bigTail.next;
cur = cur.next;
}
}
smallTail.next = bigHead.next;
return smallHead.next;
}
}
2.在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针
public class Solution {