1. Write the code to a given value x is divided into two parts, reference will list all less than x nodes ahead of nodes greater than or equal to 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. In a sorted linked list nodes duplicate, delete the duplicate node list, the node does not retain repeated, returns the list head pointer
public class Solution {
public ListNode deleteDuplication(ListNode pHead) {
if(pHead == null) {
return null;
}
if(pHead.next == null) {
return pHead;
}
ListNode newHead = new ListNode(1);
newHead.next = pHead;
ListNode prev = newHead;
ListNode node = prev.next;
while(node != null) {
if(node.next != null && node.val == node.next.val) {
while(node.next != null && node.val == node.next.val) {
node = node.next;
}
prev.next = node.next;
node = node.next;
}else {
prev = prev.next;
node = node.next;
}
}
return newHead.next;
}
}
3. determining the list palindrome
public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
ListNode B = reverseList(A);
while(A != null) {
if(A.val != B.val) {
return false;
}
A = A.next;
B = B.next;
}
return true;
}
public ListNode reverseList(ListNode head) {
if(head == null) {
return null;
}
if(head.next == null) {
return head;
}
ListNode newHead = null;
ListNode cur = head;
ListNode prev = null;
while(cur != null) {
ListNode next = cur.next;
if(next == null) {
newHead = cur;
}
cur.next = prev;
prev = cur;
cur = next;
}
return newHead;
}
}
4. Enter the two lists, find their first common node
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lenA = size(headA);
int lenB = size(headB);
if(lenA > lenB) {
int offset = lenA-lenB;
for(int i = 0; i < offset;i++) {
headA = headA.next;
}
}else {
int offset = lenB - lenA;
for(int i = 0;i < offset;i++) {
headB = headB.next;
}
}
while(headA != null && headB != null) {
if(headA == headB) {
return headA;
}
headA = headA.next;
headB = headB.next;
}
return null;
}
public int size(ListNode head) {
int size = 0;
for(ListNode cur = head;cur != null; cur = cur.next) {
size++;
}
return size;
}
}
5. Given a list, the list is determined whether a ring
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null) {
return false;
}
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if(fast == slow) {
return true;
}
}
return false;
}
}
6. Given a list, the list starts to return into the first ring node. If chain acyclic, null is returned
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null) {
return null;
}
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if(fast == slow) {
break;
}
}
if(fast == null || fast.next == null) {
return null;
}
ListNode cur1 = head;
ListNode cur2 = fast;
while(cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
}