File tree Expand file tree Collapse file tree 4 files changed +196
-0
lines changed Expand file tree Collapse file tree 4 files changed +196
-0
lines changed Original file line number Diff line number Diff line change 1+ # Definition for singly-linked list.
2+ # class ListNode(object):
3+ # def __init__(self, x):
4+ # self.val = x
5+ # self.next = None
6+
7+ class Solution (object ):
8+ def detectCycle (self , head ):
9+ """
10+ :type head: ListNode
11+ :rtype: ListNode
12+ """
13+ if head is None or head .next is None or head .next .next is None :
14+ return
15+ p1 = head .next
16+ p2 = head .next .next
17+ while (p2 is not None and p2 .next is not None and p2 .next .next is not None ):
18+ p1 = p1 .next
19+ p2 = p2 .next .next
20+ if p1 is p2 :
21+ p1 = head
22+ while p1 is not p2 :
23+ p1 = p1 .next
24+ p2 = p2 .next
25+ return p1
26+ return
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: yuanyunxu
5+ * Date: 19/4/16
6+ * Time: ÏÂÎç3:41
7+ */
8+
9+ /**
10+ * Definition for a singly-linked list.
11+ * class ListNode {
12+ * public $val = 0;
13+ * public $next = null;
14+ * function __construct($val) { $this->val = $val; }
15+ * }
16+ */
17+
18+ class ListNode {
19+ public $ val = 0 ;
20+ public $ next = null ;
21+ function __construct ($ val ) { $ this ->val = $ val ; }
22+ }
23+
24+ class Solution {
25+
26+ /**
27+ * @param ListNode $l1
28+ * @param ListNode $l2
29+ * @return ListNode
30+ */
31+ function mergeTwoLists ($ l1 , $ l2 ) {
32+ if ($ l1 == null ) {
33+ return $ l2 ;
34+ }
35+
36+ if ($ l2 == null ) {
37+ return $ l1 ;
38+ }
39+
40+ $ first = $ l1 ;
41+ $ second = $ l2 ;
42+
43+ if ($ first ->val <= $ second ->val ) {
44+ $ head = $ first ;
45+ $ first = $ first ->next ;
46+ } else {
47+ $ head = $ second ;
48+ $ second = $ second ->next ;
49+ }
50+
51+ $ now = $ head ;
52+
53+ while (true ) {
54+ if ($ first == null ) {
55+ $ now ->next = $ second ;
56+ break ;
57+ }
58+ if ($ second == null ) {
59+ $ now ->next = $ first ;
60+ break ;
61+ }
62+
63+ if ($ first ->val <= $ second ->val ) {
64+ $ now ->next = $ first ;
65+ $ first = $ first ->next ;
66+ $ now = $ now ->next ;
67+ continue ;
68+ }
69+ $ now ->next = $ second ;
70+ $ second = $ second ->next ;
71+ $ now = $ now ->next ;
72+ }
73+ return $ head ;
74+ }
75+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Definition for a singly-linked list.
4+ * class ListNode {
5+ * public $val = 0;
6+ * public $next = null;
7+ * function __construct($val) { $this->val = $val; }
8+ * }
9+ */
10+ class ListNode {
11+ public $ val = 0 ;
12+ public $ next = null ;
13+ function __construct ($ val ) { $ this ->val = $ val ; }
14+ }
15+
16+ class Solution {
17+
18+ /**
19+ * @param ListNode $head
20+ * @return ListNode
21+ */
22+ function swapPairs ($ head ) {
23+
24+ //没有元素,或者只有一个元素
25+ if ($ head == null || $ head ->next == null ) {
26+ return $ head ;
27+ }
28+ $ shaoBing = new ListNode (null );
29+ $ shaoBing ->next = $ head ->next ;
30+
31+ //到这里至少链表中至少有两个元素
32+ $ prev = $ head ;
33+ $ prior = $ prev ->next ;
34+ $ next = $ prior ->next ;
35+
36+ while ($ next && $ next ->next ) {
37+ $ prev ->next = $ next ->next ;
38+ $ prior ->next = $ prev ;
39+ $ prev = $ next ;
40+ $ prior = $ next ->next ;
41+ $ next = $ prior ->next ;
42+ }
43+
44+ //最后交换末尾两个元素, 针对单数个链表元素做处理
45+ $ prev ->next = $ next ? $ next : null ;
46+ $ prior ->next = $ prev ;
47+ return $ shaoBing ->next ;
48+ }
49+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: yuanyunxu
5+ * Date: 19/4/16
6+ * Time: ÏÂÎç3:15
7+ */
8+
9+ /**
10+ * Definition for a singly-linked list.
11+ * class ListNode {
12+ * public $val = 0;
13+ * public $next = null;
14+ * function __construct($val) { $this->val = $val; }
15+ * }
16+ */
17+
18+ class ListNode {
19+ public $ val = 0 ;
20+ public $ next = null ;
21+ function __construct ($ val ) { $ this ->val = $ val ; }
22+ }
23+
24+ class Solution {
25+
26+ /**
27+ * @param ListNode $head
28+ * @return ListNode
29+ */
30+ function deleteDuplicates ($ head ) {
31+ if ($ head == null ) {
32+ return $ head ;
33+ }
34+ $ prior = $ head ;
35+ while ($ prior ->next ) {
36+ $ next = $ prior ->next ;
37+ if ($ prior ->val == $ next ->val ) {
38+ $ prior ->next = $ next ->next ;
39+ $ next ->next = null ;
40+ continue ;
41+ }
42+ $ prior = $ prior ->next ;
43+ }
44+ return $ head ;
45+ }
46+ }
You can’t perform that action at this time.
0 commit comments