链表


NC 33  合并有序链表

题目描述:将两个有序的链表合并为一个新链表,要求新的链表是通过拼接两个链表的节点来生成的,且合并后新链表依然有序。

示例1
输入--{1},{2}
返回值--{1,2}
 
示例2
输入--{2},{1}
返回值--{1,2}
 1 # class ListNode:
 2 #     def __init__(self, x):
 3 #         self.val = x
 4 #         self.next = None
 5 
 6 #
 7 # 
 8 # @param l1 ListNode类 
 9 # @param l2 ListNode类 
10 # @return ListNode类
11 #
12 class Solution:
13     def mergeTwoLists(self , l1 , l2 ):
14         # write code here
15         if not l1:
16             return l2
17         if not l2:
18             return l1
19         
20         fake_node = ListNode(0)
21         phead = fake_node
22         phead1 = l1
23         phead2 = l2
24         
25         while phead1 and phead2:
26             if phead1.val < phead2.val:
27                 phead.next = phead1
28                 phead1 =  phead1.next
29             else:
30                 phead.next = phead2
31                 phead2 =  phead2.next
32             phead = phead.next
33             
34         while phead1:
35             phead.next = phead1
36             phead1 =  phead1.next
37             phead = phead.next
38             
39         while phead2:
40             phead.next = phead2
41             phead2 =  phead2.next
42             phead = phead.next
43         
44         return fake_node.next
45         
View Code

NC51  合并k个已排序的链表

题目描述

合并 k 个已排序的链表并将其作为一个已排序的链表返回。分析并描述其复杂度。  
示例1
输入--[{1,2,3},{4,5,6,7}]
返回值--{1,2,3,4,5,6,7}
 1 # class ListNode:
 2 #     def __init__(self, x):
 3 #         self.val = x
 4 #         self.next = None
 5 
 6 #
 7 # 
 8 # @param lists ListNode类一维数组 
 9 # @return ListNode类
10 #
11 class Solution:
12     
13     def mergeKLists(self , lists ):
14         # write code here
15         lists_len = len(lists)
16         if lists_len == 0:
17             return None
18         elif lists_len == 1:
19             return lists[0]
20         
21         first_head = self._mergeTwoLists(lists[0], lists[1])
22         for index in range(2, len(lists)):
23             temp_head = self._mergeTwoLists(first_head, lists[index])
24             first_head = temp_head
25             
26         return first_head
27             
28     def _mergeTwoLists(self , l1 , l2 ):
29         # write code here
30         if not l1:
31             return l2
32         if not l2:
33             return l1
34         
35         fake_node = ListNode(0)
36         phead = fake_node
37         phead1 = l1
38         phead2 = l2
39         
40         while phead1 and phead2:
41             if phead1.val < phead2.val:
42                 phead.next = phead1
43                 phead1 =  phead1.next
44             else:
45                 phead.next = phead2
46                 phead2 =  phead2.next
47             phead = phead.next
48             
49         while phead1:
50             phead.next = phead1
51             phead1 =  phead1.next
52             phead = phead.next
53             
54         while phead2:
55             phead.next = phead2
56             phead2 =  phead2.next
57             phead = phead.next
58         
59         return fake_node.next
View Code

 NC 78 反转链表

题目描述

输入一个链表,反转链表后,输出新链表的表头。

示例1

输入 {1,2,3}

返回值 {3,2,1}

 1 # -*- coding:utf-8 -*-
 2 # class ListNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6 class Solution:
 7     # 返回ListNode
 8     def ReverseList(self, pHead):
 9         # write code here
10         back = None
11         front = pHead
12         while front != None:
13             temp = front
14             front = front.next
15             temp.next = back
16             back = temp
17             
18         return back
View Code

NC50  链表中的节点每k个一组翻转

题目描述

将给出的链表中的节点每\ k k 个一组翻转,返回翻转后的链表
如果链表中的节点数不是\ k k 的倍数,将最后剩下的节点保持原样
你不能更改节点中的值,只能更改节点本身。
要求空间复杂度 \ O(1) O(1)
 
例如:
给定的链表是1\to2\to3\to4\to512345
对于 \ k = 2 k=2, 你应该返回 2\to 1\to 4\to 3\to 521435
对于 \ k = 3 k=3, 你应该返回 3\to2 \to1 \to 4\to 532145
 
 1 # class ListNode:
 2 #     def __init__(self, x):
 3 #         self.val = x
 4 #         self.next = None
 5 
 6 # @param head ListNode类 
 7 # @param k int整型 
 8 # @return ListNode类
 9 #
10 class Solution:
11     def reverseKGroup(self , head , k ):
12         k_th_node = head
13         for i in range(k):
14             if not k_th_node:
15                 return head
16             k_th_node = k_th_node.next
17             
18         new_head = self._reverse(head, k_th_node)
19         head.next = self.reverseKGroup(k_th_node, k)
20         
21         return new_head
22 
23     def _reverse(self, head, tail):
24         back = None
25         front = head
26         while front != tail:
27             temp = front
28             front = front.next
29             temp.next = back
30             back = temp
31         
32         return back
View Code

 NC24 删除有序链表中重复出现的元素

题目描述

给出一个升序排序的链表,删除链表中的所有重复出现的元素,只保留原链表中只出现一次的元素。
例如:
给出的链表为1 \to 2\to 3\to 3\to 4\to 4\to51233445, 返回1\to 2\to5125.
给出的链表为1\to1 \to 1\to 2 \to 311123, 返回2\to 323.
示例1

输入:{1,2,2}

返回值:{1}

 1 # class ListNode:
 2 #     def __init__(self, x):
 3 #         self.val = x
 4 #         self.next = None
 5 
 6 #
 7 # 
 8 # @param head ListNode类 
 9 # @return ListNode类
10 #
11 class Solution:
12     def deleteDuplicates(self , head ):
13         # write code here
14         if not head:
15             return None
16         fake_node = ListNode(0)
17         back = fake_node
18         phead = head
19         
20         while phead:
21             left = phead
22             compare_value = left.val
23             right = left
24 
25             while right:
26                 if right.val == compare_value:
27                     right = right.next
28                 else:
29                     break
30                     
31             if right == left.next:
32                 back.next = left
33                 back = back.next
34                 phead = right
35             else:
36                 phead = right
37                 
38         back.next = None
39         
40         return fake_node.next
View Code

 

posted @ 2021-02-07 14:39  aiwenhua_0610  阅读(50)  评论(0编辑  收藏  举报