Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to delete duplicate elements in a sorted linked list

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use java to delete the repeated elements in the sorted list. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Given a sorted linked list, delete all nodes that contain duplicate numbers, leaving only those numbers that are not repeated in the original linked list.

Example 1:

Enter: 1-> 2-> 3-> 3-> 4-> 4-> 5

Output: 1-> 2-> 5

Example 2:

Enter: 1-> 1-> 1-> 2-> 3

Output: 2-> 3

Answer:

1public ListNode deleteDuplicates1 (ListNode head) {

2 if (head = = null) return null

3 ListNode FakeHead = new ListNode (0)

4 FakeHead.next = head

5 ListNode pre = FakeHead

6 ListNode cur = head

7 while (cur! = null) {

8 while (cur.next! = null & & cur.val = = cur.next.val) {

9 cur = cur.next

10}

11 if (pre.next = = cur) {

12 pre = pre.next

13} else {

14 pre.next = cur.next

15}

16 cur = cur.next

17}

18 return FakeHead.next

19}

Parsing:

Because it is sorting, this is easy. If the previous node is the same as the next node, make sure that the current node is deleted when you delete the next node. This problem is not very difficult, and there are many solutions. Let's take a look at a recursive solution.

1public ListNode deleteDuplicates2 (ListNode head) {

2 if (head = = null | | head.next = = null) return head

3 if (head.val! = head.next.val) {

4 head.next = deleteDuplicates2 (head.next)

5 return head

6} else {

7 while (head.next! = null & & head.val = = head.next.val)

8 head = head.next

9 return deleteDuplicates2 (head.next)

10}

11}

This kind of recursion is more concise, and the page is a little bit easier to understand.

What are the characteristics of Java? what are the characteristics of Java? what is the 1.Java language that represents the static object-oriented programming language? it implements the object-oriented theory and allows programmers to carry out complex programming in an elegant way of thinking. 2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, dynamic and so on. 3. Using Java, you can write desktop applications, Web applications, distributed systems, embedded system applications, and so on.

On "how to use java to delete repeated elements in the sorted list" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report