forked from EasonLeeee/TargetOffer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gatsbydhn
committed
Jul 11, 2016
1 parent
454a450
commit 848a332
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 输入一个链表,输出该链表中倒数第k个结点。 | ||
*/ | ||
|
||
/** | ||
* 特殊值测试:1、头指针为空 2、总结点数少于k 3、k等于0 | ||
*/ | ||
|
||
public class Solution { | ||
public ListNode FindKthToTail(ListNode head,int k) { | ||
|
||
ListNode ans = head; | ||
|
||
if (head == null || k < 1) { | ||
return null; | ||
} | ||
int i = k; | ||
//head先前进k-1个节点 | ||
while (i > 1 && head.next != null) { | ||
head = head.next; | ||
i--; | ||
} | ||
//节点总数不够 | ||
if (i != 1) { | ||
return null; | ||
} | ||
|
||
while (head.next != null) { | ||
head = head.next; | ||
ans = ans.next; | ||
} | ||
|
||
return ans; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* 输入一个链表,反转链表后,输出链表的所有元素。 | ||
*/ | ||
|
||
/** | ||
* 思路:使用三个指针last,head,next,分别指向当前节点前一个节点,当前节点,当前节点下一个节点。初始时,last指向null。时间复杂度:O(n) | ||
* 特殊值测试:1、head为空 2、只有一个节点 | ||
*/ | ||
|
||
|
||
public class Solution { | ||
public ListNode ReverseList(ListNode head) { | ||
if (head == null) | ||
return null; | ||
|
||
ListNode last = null; | ||
ListNode next = null; | ||
while (head != null) { | ||
next = head.next; | ||
head.next = last; | ||
last = head; | ||
head = next; | ||
if (head == null) { | ||
return last; | ||
} | ||
} | ||
return null; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则 | ||
*/ | ||
|
||
/** | ||
* 时间复杂度:O(n) | ||
* 特殊值测试:1、链表都为空 2、其中一个为空 | ||
*/ | ||
|
||
public class Solution { | ||
public ListNode Merge(ListNode list1,ListNode list2) { | ||
if (list1 == null) { | ||
return list2; | ||
} else if (list2 == null) { | ||
return list1; | ||
} | ||
|
||
ListNode newHead = null; | ||
while (list1 != null && list2 != null) { | ||
if (list1.val <= list2.val) { | ||
if (newHead == null) { | ||
newHead = list1; | ||
} | ||
ListNode last = list1; | ||
list1 = list1.next; | ||
last.next = list2; | ||
} else { | ||
if (newHead == null) { | ||
newHead = list2; | ||
} | ||
ListNode last = list2; | ||
list2 = list2.next; | ||
last.next = list1; | ||
} | ||
} | ||
return newHead; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空) | ||
*/ | ||
|
||
/** | ||
* 思路:将新创建的节点添加到原节点的后面,最后一步分离。时间复杂度O(n); | ||
* 特殊输入测试:1、头结点指向null 2.只有一个节点 | ||
*/ | ||
|
||
|
||
public class Solution { | ||
public RandomListNode Clone(RandomListNode pHead) | ||
{ | ||
if (pHead == null) { | ||
return null; | ||
} | ||
//将拷贝的节点添加到原节点的后面 | ||
RandomListNode pCur = pHead; | ||
while (pCur != null) { | ||
RandomListNode newNode = new RandomListNode(pCur.label); | ||
RandomListNode last = pCur; | ||
pCur = pCur.next; | ||
newNode.next = last.next; | ||
last.next = newNode; | ||
} | ||
|
||
//进行random指针的拷贝 | ||
pCur = pHead; | ||
while (pCur != null) { | ||
if (pCur.random != null) { | ||
pCur.next.random = pCur.random.next; | ||
} | ||
pCur = pCur.next.next; | ||
} | ||
|
||
//将复制的节点从原链表中分离 | ||
pCur = pHead; | ||
RandomListNode pNewHead = pCur.next; | ||
RandomListNode pNewCur = pNewHead; | ||
while (pCur != null) { | ||
pCur.next = pNewCur.next; | ||
if (pNewCur.next == null) { | ||
return pNewHead; | ||
} | ||
pNewCur.next = pNewCur.next.next; | ||
pCur = pCur.next; | ||
pNewCur = pNewCur.next; | ||
} | ||
|
||
return pNewHead; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* 输入一个链表,从尾到头打印链表每个节点的值。 | ||
*/ | ||
|
||
import java.util.ArrayList; | ||
public class Solution { | ||
ArrayList<Integer> list = new ArrayList<Integer>(); | ||
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { | ||
recursivePrint(listNode); | ||
return list; | ||
} | ||
|
||
public void recursivePrint(ListNode listNode) { | ||
if (listNode == null) { | ||
return; | ||
} | ||
recursivePrint(listNode.next); | ||
list.add(listNode.val); | ||
} | ||
} |