题目描述
Given a singly linked list L: L 0→L 1→…→L n-1→L n,
reorder it to: L 0→L n →L 1→L n-1→L 2→L n-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}.
public class Solution {
public void reorderList(ListNode head) {
if(head == null || head.next == null) {
return;
}
ListNode slow = head;
ListNode quick = head;
while(quick.next != null && quick.next.next != null) {
slow = slow.next;
quick = quick.next.next;
}
ListNode mid = slow.next;
if(mid != null && mid.next != null) {
ListNode next = mid.next;
while(next != null) {
mid.next = next.next;
next.next = slow.next;
slow.next = next;
next = mid.next;
}
}
ListNode p = head;
ListNode q = slow.next;
while(q != null && p != null){
slow.next = q.next;
q.next = p.next;
p.next = q;
p = p.next.next;
q = slow.next;
}
}
}
题目描述
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Follow up:
Can you solve it without using extra space?
public class Solution {
public ListNode detectCycle(ListNode head) {
if (head != null) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
fast = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
}
return null;
}
}
题目描述
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
public class Solution {
public boolean hasCycle(ListNode head) {
if(head != null) {
ListNode slow = head;
ListNode fast = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if(fast == slow) {
return true;
}
}
}
return false;
}
}
题目描述
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s ="leetcode",
dict =["leet", "code"].
Return true because"leetcode"can be segmented as"leet code".
public class Solution {
public boolean wordBreak(String s, Set<String> dict) {
boolean[] temp = new boolean[s.length() + 1];
temp[0] = true;
for(int i = 1; i < temp.length; i++) {
for(int j = 0; j < i; j++) {
if (temp[j] && dict.contains(s.substring(j, i))){
temp[i] = true;
break;
}
}
}
return temp[temp.length - 1];
}
}
题目描述
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if(head == null || (head.next == null && head.random == null)){
return head;
}
RandomListNode node = head;
while(node != null){
RandomListNode copy = new RandomListNode(node.label);
copy.next = node.next;
copy.random = node.random;
node.next = copy;
node = node.next.next;
}
node = head.next;
while(node != null){
if(node.next != null){
node.next = node.next.next;
}
if(node.random != null){
node.random = node.random.next;
}
node = node.next;
}
return head.next;
}
}