My code:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode dummy = new ListNode(Integer.MIN_VALUE);
dummy.next = head;
ListNode pre = dummy;
ListNode curr = pre.next;
ListNode post = curr.next;
boolean isDuplicated = false;
while (curr != null) {
if (post == null) {
if (isDuplicated) {
curr = null;
pre.next = null;
}
else
break;
}
else if (curr.val == post.val) {
curr.next = post.next;
post = curr.next;
isDuplicated = true;
}
else if (curr.val != post.val) {
if (isDuplicated) {
pre.next = curr.next;
curr = post;
post = curr.next;
isDuplicated = false;
}
else {
pre = curr;
curr = post;
post = post.next;
}
}
}
return dummy.next;
}
}
这次题目不难。
这是我当时画的图。
看了就基本清楚了。当然还需要分成两种情况
如果是发现重复,并且已经删除完了,那么pre不需要移动。否则就要移动一格。
同样的,如果发现post已经是null了,可能是两种情况。
现在过来看,还是代码写的太复杂。感觉是那几天睡眠不好,所以脑子是糊涂得。
简化代码如下:
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode dummy = new ListNode(Integer.MIN_VALUE);
dummy.next = head;
ListNode pre = dummy;
ListNode slow = pre.next;
ListNode fast = slow.next;
while (fast != null) {
ListNode temp = fast.next;
pre.next = fast;
fast.next = slow;
slow.next = temp;
pre = slow;
slow = pre.next;
if (slow == null)
break;
fast = slow.next;
}
return dummy.next;
}
**
总结: 链表操作
当需要进行删除操作,交换操作,涉及前后两个结点的时候,
可以用 pre 指向头一个结点的前一个结点。
curr指向头一个结点,
detect指向下一个或几个结点。
然后循环的判断条件就是,detect是否为空。
然后再加上一些实现细节,根据不同的题目。
**
Anyway, Good luck, Richardo!
My code:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
ListNode curr = head;
while (curr != null) {
if (curr.next != null && curr.val == curr.next.val) { // if current value = next value
curr = curr.next;
}
else {
if (pre.next != curr) { // if previous inequal node is not ajacent to current one, start to delete
pre.next = curr.next;
curr = pre.next;
}
else { // if previous inequal node is ajacent to current one, move on
pre = pre.next;
curr = curr.next;
}
}
}
return dummy.next;
}
}
第一个版本的代码好复杂。这次写的感觉还是比较明确简洁啊。
也没什么好说的。
Anyway, Good luck, Richardo!
My code:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode curr = head;
ListNode pre = dummy;
ListNode post = head.next;
while (post != null) {
if (post.val == curr.val) {
post = post.next;
}
else if (post != curr.next) {
pre.next = post;
curr = post;
post = post.next;
}
else {
pre = pre.next;
curr = curr.next;
post = post.next;
}
}
if (curr.next != null) {
pre.next = null;
}
return dummy.next;
}
}
类似于三个指针,然后删除操作。
I 的情况是双指针。
不难。
Anyway, Good luck, Richardo! -- 08/16/2016