Skip to content

Commit

Permalink
add t57 t56 t37
Browse files Browse the repository at this point in the history
  • Loading branch information
gatsbydhn committed Jul 4, 2016
1 parent 649e4b3 commit 815eecf
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
53 changes: 53 additions & 0 deletions 链表/t36_两个链表的第一个公共节点.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* 输入两个链表,找出它们的第一个公共结点。
*/

/**
* 特殊值测试:有一个链表为空
* 特殊值测试:无公共链表
*/

public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
int len1 = getLength(pHead1);
int len2 = getLength(pHead2);
if (len1 == 0 || len2 == 0) {
return null;
}
//计算链表差值
int dif = len1 > len2 ? len1 - len2 : len2 - len1;
//长的链表先走dif步
if (len1 > len2) {
for (int i = 0; i < dif; i++) {
pHead1 = pHead1.next;
}
} else {
for (int i = 0; i < dif; i++) {
pHead2 = pHead2.next;
}
}

//一起往前走
while (pHead1 != null && pHead2 != null && pHead1 != pHead2) {
pHead1 = pHead1.next;
pHead2 = pHead2.next;
}

if (pHead1 == null || pHead2 == null) {
return null;
} else {
return pHead1;
}

}

//获取链表长度
int getLength(ListNode pHead) {
int len = 0;
while (pHead != null) {
len++;
pHead = pHead.next;
}
return len;
}
}
62 changes: 62 additions & 0 deletions 链表/t56_链表中环的入口节点.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* 一个链表中包含环,请找出该链表的环的入口结点。
*/

/**
* 特殊输入:1.无环 2.整个链表为一个环
*/

public class Solution {

public ListNode EntryNodeOfLoop(ListNode pHead)
{
if (pHead == null)
return null;
if (pHead.next == pHead)
return pHead;
//寻找环中的一个节点
ListNode node = nodeInLoop(pHead);
if (node == null)
return null;

//计算环中元素个数
ListNode node1 = node.next;
int count = 1;
while (node1 != node) {
count++;
node1 = node1.next;
}
//寻找入口
ListNode ahead = pHead;
ListNode behind = pHead;
for (int i = 0; i < count; i++) {
ahead = ahead.next;
}
while (ahead != behind) {
ahead = ahead.next;
behind = behind.next;
}
return ahead;


}
//通过一快一慢两个指针寻找环中的一个节点,无环时返回null
public ListNode nodeInLoop(ListNode pHead) {
if (pHead == null || pHead.next == null) {
return null;
}
ListNode slow = pHead;
ListNode fast = pHead;
slow = slow.next;
fast = fast.next.next;
while (fast != null && fast.next != null && slow != fast) {
slow = slow.next;
fast = fast.next.next;
}
if (fast == slow)
return fast;
else
return null;

}
}
64 changes: 64 additions & 0 deletions 链表/t57_删除链表中重复的节点.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
在一个排序的链表中存在重复的结点请删除该链表中重复的结点重复的结点不保留返回链表头指针例如链表1->2->3->3->4->4->5 处理后为 1->2->5

/**
* 测试用例: 1->1->2->3->3
**/
public class Solution {
public ListNode deleteDuplication(ListNode pHead)
{
if (pHead == null) {
return null;
}
ListNode pre = null;
ListNode cur = pHead;
while (cur != null) {
ListNode next = cur.next;
boolean needDelete = false;
//判读是否删除当前节点
if (next != null && cur.val == next.val)
needDelete = true;
if (!needDelete) {
pre = cur;
cur = next;
} else {
//cur后移,直到为null或指向不重复元素
while (cur.next != null && cur.val == next.val) {
cur = next;
next = cur.next;
}
cur = next;
//更新pre
if (pre == null) {
pHead = cur;
} else {
pre.next = cur;
}

}
}
return pHead;
}
}


//除去重复元素,仅保留一个重复元素版本
public class Solution {
public ListNode deleteDuplication(ListNode pHead)
{
if (pHead == null) {
return null;
}
ListNode cur = pHead;
while (cur.next != null) {
while (cur.next != null) {
if (cur.val == cur.next.val) {
cur.next = cur.next.next;
} else {
break;
}
}
cur = cur.next;
}
return pHead;
}
}

0 comments on commit 815eecf

Please sign in to comment.